Skip to content

Commit

Permalink
Merge pull request #46 from jbclaudio/develop
Browse files Browse the repository at this point in the history
Improve code to PhpStan level 4 + Akeneo Connector version constraint
  • Loading branch information
jbclaudio authored Sep 25, 2024
2 parents df870ff + ff9c62a commit efade77
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Block/Adminhtml/Akeneo/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function _construct(): void
/**
* Retrieve text for header element depending on loaded post
*/
public function getHeaderText(): Phrase
public function getHeaderText(): Phrase|string
{
if ($this->coreRegistry->registry('akeneo')->getId()) {
return __("Edit Akeneo '%1'", $this->escapeHtml($this->coreRegistry->registry('akeneo')->getTitle()));
Expand Down
38 changes: 14 additions & 24 deletions Block/Adminhtml/Akeneo/Edit/Tab/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace JustBetter\AkeneoBundle\Block\Adminhtml\Akeneo\Edit\Tab;

use IntlDateFormatter;
use JustBetter\AkeneoBundle\Block\Adminhtml\Akeneo\Grid;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Magento\Framework\Registry;
use Magento\Store\Model\System\Store;
use Magento\Framework\Data\FormFactory;
Expand Down Expand Up @@ -31,16 +33,13 @@ public function __construct(
/**
* Prepare form
*
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @throws LocalizedException
*/
protected function _prepareForm(): static
{
$model = $this->_coreRegistry->registry('akeneo');

$isElementDisabled = false;

$form = $this->_formFactory->create();

$form->setHtmlIdPrefix('page_');
Expand All @@ -59,8 +58,8 @@ protected function _prepareForm(): static
'title' => __('Type'),
'name' => 'import',
'required' => true,
'options' => \JustBetter\AkeneoBundle\Block\Adminhtml\Akeneo\Grid::getOptionArray0(),
'disabled' => $isElementDisabled
'options' => Grid::getOptionArray0(),
'disabled' => false
]
);

Expand All @@ -72,7 +71,7 @@ protected function _prepareForm(): static
'label' => __('Code'),
'title' => __('Code'),
'required' => true,
'disabled' => $isElementDisabled
'disabled' => false
]
);

Expand All @@ -84,7 +83,7 @@ protected function _prepareForm(): static
'label' => __('Magento Entity ID'),
'title' => __('Magento Entity ID'),
'required' => true,
'disabled' => $isElementDisabled
'disabled' => false
]
);

Expand All @@ -103,14 +102,12 @@ protected function _prepareForm(): static
'label' => __('Created'),
'title' => __('Created'),
'date_format' => $dateFormat,
//'time_format' => $timeFormat,

'disabled' => $isElementDisabled,
'disabled' => false
]
);

if (!$model->getId()) {
$model->setData('is_active', $isElementDisabled ? '0' : '1');
$model->setData('is_active', '1');
}

$form->setValues($model->getData());
Expand All @@ -121,52 +118,45 @@ protected function _prepareForm(): static

/**
* Prepare label for tab
*
* @return \Magento\Framework\Phrase
*/
public function getTabLabel()
public function getTabLabel(): Phrase|string
{
return __('Item Information');
}

/**
* Prepare title for tab
*
* @return \Magento\Framework\Phrase
*/
public function getTabTitle()
public function getTabTitle(): Phrase|string
{
return __('Item Information');
}

/**
* {@inheritdoc}
*/
public function canShowTab()
public function canShowTab(): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function isHidden()
public function isHidden(): bool
{
return false;
}

/**
* Check permission for passed action
*
* @param string $resourceId
* @return bool
*/
protected function _isAllowedAction($resourceId)
protected function _isAllowedAction(string $resourceId): bool
{
return $this->_authorization->isAllowed($resourceId);
}

public function getTargetOptionArray()
public function getTargetOptionArray(): array
{
return ['_self' => 'Self', '_blank' => 'New Page'];
}
Expand Down
7 changes: 5 additions & 2 deletions Block/Adminhtml/Akeneo/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JustBetter\AkeneoBundle\Block\Adminhtml\Akeneo;

use Exception;
use Magento\Backend\Block\Widget\Grid\Massaction;
use Magento\Backend\Helper\Data;
use Magento\Framework\Module\Manager;
use Magento\Backend\Block\Template\Context;
Expand Down Expand Up @@ -116,7 +117,9 @@ protected function _prepareMassaction(): static
$this->setMassactionIdField('id');
$this->getMassactionBlock()->setFormFieldName('akeneo');

$this->getMassactionBlock()->addItem(
/** @var Massaction $massactionBlock */
$massactionBlock = $this->getMassactionBlock();
$massactionBlock->addItem(
'delete',
[
'label' => __('Delete'),
Expand All @@ -127,7 +130,7 @@ protected function _prepareMassaction(): static

$statuses = $this->status->getOptionArray();

$this->getMassactionBlock()->addItem(
$massactionBlock->addItem(
'status',
[
'label' => __('Change status'),
Expand Down
6 changes: 1 addition & 5 deletions Block/Adminhtml/System/Config/Form/Field/TaxIdMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ public function renderCellTemplate($columnName): string
return parent::renderCellTemplate($columnName);
}

$options = [];

if (isset($this->_columns[$columnName])) {
$options = $this->productTaxClassSource->getAllOptions();
}
$options = $this->productTaxClassSource->getAllOptions();

/** @var Select $element */
$element = $this->elementFactory->create('select');
Expand Down
8 changes: 7 additions & 1 deletion Console/Command/ImportMetricUnits.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JustBetter\AkeneoBundle\Console\Command;

use Exception;
use JustBetter\AkeneoBundle\Job\ImportMetricUnits as ImportMetricUnitsJob;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -23,8 +24,13 @@ protected function configure(): void
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): void
/**
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->job->execute($output);

return 0;
}
}
4 changes: 3 additions & 1 deletion Console/Command/SetNotVisible.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ protected function configure(): void
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Starting');

$this->job->execute($output);

$output->writeln('Finished!');

return 0;
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use JustBetter\AkeneoBundle\Block\Adminhtml\Akeneo\Grid;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Block\Widget\Grid\Export;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\ResponseInterface;
Expand All @@ -27,6 +28,7 @@ public function execute(): ResultInterface|ResponseInterface
{
$this->_view->loadLayout(false);

/** @var Export $exportBlock */
$exportBlock = $this->_view->getLayout()->createBlock(Grid::class);

return $this->fileFactory->create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use JustBetter\AkeneoBundle\Block\Adminhtml\Akeneo\Grid;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Block\Widget\Grid\Export;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\ResponseInterface;
Expand All @@ -27,6 +28,7 @@ public function execute(): ResultInterface|ResponseInterface
{
$this->_view->loadLayout(false);

/** @var Export $exportBlock */
$exportBlock = $this->_view->getLayout()->createBlock(Grid::class);

return $this->fileFactory->create(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace JustBetter\AkeneoBundle\Controller\Adminhtml\Akeneo;

use JustBetter\AkeneoBundle\Model\Akeneo;
use Magento\Backend\App\Action;
use Magento\Backend\Model\Session;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\PhpEnvironment\Request;

class Save extends Action
{
Expand All @@ -16,14 +20,16 @@ public function __construct(

public function execute(): ResultInterface
{
$data = $this->getRequest()->getPostValue();
/** @var Request $request */
$request = $this->getRequest();
$data = $request->getPostValue();

/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
$model = $this->_objectManager->create(\JustBetter\AkeneoBundle\Model\Akeneo::class);
$model = $this->_objectManager->create(Akeneo::class);

$id = $this->getRequest()->getParam('id');
$id = $request->getParam('id');
if ($id) {
$model->load($id);
$model->setCreatedAt(date('Y-m-d H:i:s'));
Expand All @@ -34,19 +40,19 @@ public function execute(): ResultInterface
try {
$model->save();
$this->messageManager->addSuccess(__('The Akeneo has been saved.'));
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData(false);
if ($this->getRequest()->getParam('back')) {
$this->_objectManager->get(Session::class)->setFormData(false);
if ($request->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
}
return $resultRedirect->setPath('*/*/');
} catch (\Magento\Framework\Exception\LocalizedException|\RuntimeException $e) {
} catch (LocalizedException|\RuntimeException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the Akeneo.'));
}

$this->_getSession()->setFormData($data);
return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
return $resultRedirect->setPath('*/*/edit', ['id' => $request->getParam('id')]);
}

return $resultRedirect->setPath('*/*/');
Expand Down
9 changes: 6 additions & 3 deletions Helper/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JustBetter\AkeneoBundle\Helper\Import;

use Akeneo\Connector\Helper\Import\Product as BaseProduct;
use Magento\Framework\DB\Adapter\AdapterInterface;

class Product extends BaseProduct
{
Expand Down Expand Up @@ -43,7 +44,7 @@ protected function getFirstValue(array $values): mixed
}

/**
* Check if an attribute is scopeable or localizable based on the column result name, ex. name-nl_NL-ecommerce
* Check if an attribute is scopable or localizable based on the column result name, ex. name-nl_NL-ecommerce
*/
protected function isScopableOrLocalizable(string $attributeCode, array $columnResult): bool
{
Expand Down Expand Up @@ -71,13 +72,15 @@ protected function getRequiredAttributes(): array
$eavAttributeTable = $this->connection->getTableName('eav_attribute');
$eavEntityTypeTable = $this->connection->getTableName('eav_entity_type');

$select = $this->connection->select()
/** @var AdapterInterface $connection */
$connection = $this->connection;
$select = $connection->select()
->from("$eavAttributeTable AS attr")
->join("$eavEntityTypeTable AS type",
"attr.entity_type_id = type.entity_type_id AND type.entity_type_code = 'catalog_product'")
->where('is_required = 1');

$requiredAttributes = $this->connection->fetchAll($select);
$requiredAttributes = $connection->fetchAll($select);

return array_map(fn (array $attribute) => $attribute['attribute_code'], $requiredAttributes);
}
Expand Down
11 changes: 8 additions & 3 deletions Job/ImportMetricUnits.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Akeneo\Connector\Helper\Authenticator;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursor;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use Akeneo\Pim\ApiClient\Search\SearchBuilder;
use Exception;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -33,6 +34,9 @@ public function __construct(
}
}

/**
* @throws Exception
*/
public function execute(OutputInterface $output = null): void
{
if (! $this->akeneoClient) {
Expand All @@ -55,6 +59,7 @@ public function execute(OutputInterface $output = null): void
: $akeneoAttribute['default_metric_unit'];

try {
/** @var Attribute $magentoAttribute */
$magentoAttribute = $this->attributeRepository->get('catalog_product', $code);
} catch (NoSuchEntityException) {
$output?->writeln("<error>Skipping $code because it does not exist in Magento</error>");
Expand All @@ -66,15 +71,15 @@ public function execute(OutputInterface $output = null): void
}

$magentoAttribute->setData(self::EAV_ATTRIBUTE_UNIT_FIELD, $unit);
$magentoAttribute->save();
$magentoAttribute->save(); // @phpstan-ignore-line

$output?->writeln("Set unit for <info>$code</info> to <info>$unit</info>");
}

$output?->writeln("<info>Done</info>");
}

protected function getMetricAttributes(): ResourceCursor
protected function getMetricAttributes(): ResourceCursorInterface
{
$search = (new SearchBuilder())->addFilter('type', 'IN', ['pim_catalog_metric']);

Expand Down
Loading

0 comments on commit efade77

Please sign in to comment.