Skip to content

Commit

Permalink
fix: code issues
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Carlsen <[email protected]>
  • Loading branch information
sjelfull committed Apr 24, 2024
1 parent 9fa1677 commit ac8b743
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 43 deletions.
2 changes: 1 addition & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

$ecsConfig->parallel();
$ecsConfig->sets([SetList::CRAFT_CMS_4]);
};
};
18 changes: 8 additions & 10 deletions src/Beam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@

namespace superbig\beam;

use superbig\beam\services\BeamService as BeamServiceService;
use superbig\beam\variables\BeamVariable;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\web\twig\variables\CraftVariable;

use craft\events\RegisterUrlRulesEvent;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use superbig\beam\services\BeamService as BeamServiceService;
use superbig\beam\variables\BeamVariable;

use yii\base\Event;

Expand Down Expand Up @@ -56,23 +54,23 @@ public function init()
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
function(RegisterUrlRulesEvent $event) {
$event->rules['beam/download'] = 'beam/default';
}
);

Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function (RegisterUrlRulesEvent $event) {
function(RegisterUrlRulesEvent $event) {
$event->rules['beam/download'] = 'beam/default';
}
);

Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
function(Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('beam', BeamVariable::class);
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace superbig\beam\controllers;

use superbig\beam\Beam;

use Craft;

use craft\web\Controller;
use superbig\beam\Beam;
use yii\web\NotFoundHttpException;

/**
Expand All @@ -23,7 +23,6 @@
*/
class DefaultController extends Controller
{

protected array|int|bool $allowAnonymous = ['index'];

/**
Expand All @@ -34,15 +33,15 @@ class DefaultController extends Controller
public function actionIndex()
{
$request = Craft::$app->getRequest();
$hash = $request->getRequiredParam('hash');
$hash = $request->getRequiredParam('hash');

$config = Beam::$plugin->beamService->downloadHash($hash);

if (!$config) {
throw new NotFoundHttpException();
}

$path = $config['path'];
$path = $config['path'];
$filename = $config['filename'];

return Craft::$app->getResponse()->sendFile($path, $filename, [
Expand Down
7 changes: 3 additions & 4 deletions src/models/BeamModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

namespace superbig\beam\models;

use superbig\beam\Beam;

use Craft;
use craft\base\Model;

use superbig\beam\Beam;

/**
* @author Superbig
* @package Beam
Expand Down Expand Up @@ -71,7 +70,7 @@ public function getConfig(): array
{
return [
'header' => $this->header,
'rows' => $this->content,
'rows' => $this->content,
];
}

Expand Down
38 changes: 18 additions & 20 deletions src/services/BeamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@

namespace superbig\beam\services;

use Craft;
use craft\base\Component;
use craft\helpers\FileHelper;
use craft\helpers\Path;
use craft\helpers\StringHelper;
use craft\helpers\UrlHelper;
use superbig\beam\Beam;

use Craft;
use craft\base\Component;
use craft\helpers\UrlHelper;
use League\Csv\Writer;
use League\Csv\Reader;
use superbig\beam\Beam;
use superbig\beam\models\BeamModel;
use XLSXWriter;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\ExitException;
use yii\base\InvalidConfigException;
use yii\base\InvalidRouteException;
use yii\web\Response;

/**
* @author Superbig
Expand All @@ -51,7 +48,7 @@ public function create($config = [])
*/
public function csv(BeamModel $model): void
{
$header = $model->header;
$header = $model->header;
$content = $model->content;

if (empty($header) && empty($content)) {
Expand Down Expand Up @@ -87,8 +84,8 @@ public function csv(BeamModel $model): void
public function xlsx(BeamModel $model): void
{
$tempPath = Craft::$app->path->getTempPath() . DIRECTORY_SEPARATOR . 'beam' . DIRECTORY_SEPARATOR;
$header = $model->header;
$content = $model->content;
$header = $model->header;
$content = $model->content;

if (empty($header) && empty($content)) {
return;
Expand All @@ -99,7 +96,7 @@ public function xlsx(BeamModel $model): void
}

// Load the CSV document from a string
$writer = new XLSXWriter();
$writer = new XLSXWriter();
$sheetName = !empty($model->sheetName) ? $model->sheetName : 'Sheet';

if (!empty($header)) {
Expand Down Expand Up @@ -155,17 +152,17 @@ public function downloadHash($fileHash = null): array | bool
*/
private function writeAndRedirect(string $content, string $filename, string $mimeType): void
{
$tempPath = Craft::$app->path->getTempPath() . DIRECTORY_SEPARATOR . 'beam' . DIRECTORY_SEPARATOR;
$tempPath = Craft::$app->path->getTempPath() . DIRECTORY_SEPARATOR . 'beam' . DIRECTORY_SEPARATOR;
$tempFilename = StringHelper::randomString(12) . "-{$filename}";
$config = [
'filename' => $filename,
$config = [
'filename' => $filename,
'tempFilename' => $tempFilename,
'mimeType' => $mimeType,
'mimeType' => $mimeType,
];

$hashConfig = $this->hashConfig($config);
$verifyHash = Craft::$app->getSecurity()->hashData($hashConfig);
$url = UrlHelper::siteUrl('beam/download', [
$url = UrlHelper::siteUrl('beam/download', [
'hash' => $verifyHash,
]);

Expand All @@ -188,18 +185,19 @@ public function unhashConfig(string $hash): array
$config = base64_decode($hash);
$config = explode('||', $config);

list ($filename, $tempFilename, $mimeType) = $config;
list($filename, $tempFilename, $mimeType) = $config;

$config = [
'filename' => $filename,
'filename' => $filename,
'tempFilename' => $tempFilename,
'mimeType' => $mimeType,
'mimeType' => $mimeType,
];

return $config;
}

private function normalizeCellFormat(string $type): string {
private function normalizeCellFormat(string $type): string
{
$types = [
'number' => 'integer',
'date' => 'date',
Expand Down
4 changes: 1 addition & 3 deletions src/variables/BeamVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

use superbig\beam\Beam;

use Craft;

/**
* @author Superbig
* @package Beam
Expand All @@ -29,7 +27,7 @@ class BeamVariable
*
* @return null
*/
public function create ($options = [])
public function create($options = [])
{
return Beam::$plugin->beamService->create($options);
}
Expand Down

0 comments on commit ac8b743

Please sign in to comment.