Skip to content

Commit

Permalink
Merge pull request #21 from wedevelopnl/cleanup
Browse files Browse the repository at this point in the history
Cleanup + phpstan + rector + php 8.1 requirement
  • Loading branch information
erikfrerejean authored Feb 16, 2024
2 parents 021fbf8 + 1dbc610 commit 8fd1902
Show file tree
Hide file tree
Showing 30 changed files with 401 additions and 221 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG ALPINE_VERSION=3.16

FROM php:8.0-cli-alpine$ALPINE_VERSION AS php-cli
FROM php:8.1-cli-alpine$ALPINE_VERSION AS php-cli

RUN apk add php git make perl icu-dev --no-cache
RUN docker-php-ext-install intl
Expand All @@ -10,7 +10,7 @@ WORKDIR /app
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
COPY composer.json ./
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN composer install --prefer-dist --no-progress --no-suggest --no-interaction --no-plugins --ignore-platform-reqs
RUN composer install --prefer-dist --no-progress --no-interaction --no-plugins --ignore-platform-reqs

COPY dev/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This module adds import Akeneo data to Silverstripe enviroment

# Requirements
* SilverStripe ^4.11
* PHP ^8.0
* PHP ^8.1

# Installation
- `composer require "wedevelopnl/silverstripe-akeneo"`
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
}],
"type": "silverstripe-vendormodule",
"require": {
"php": "^8.0",
"php": "^8.1",
"silverstripe/framework": "^4.11",
"silverstripe/cms": "^4.11",
"symbiote/silverstripe-gridfieldextensions": "^3.4",
"undefinedoffset/sortablegridfield": "^2.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.4"
"friendsofphp/php-cs-fixer": "^3.4",
"rector/rector": "^0.18.2",
"wernerkrauss/silverstripe-rector": "dev-main",
"phpstan/phpstan": "1.10.54",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.0",
"syntro/silverstripe-phpstan": "1.0"
},
"extra": {
"installer-name": "silverstripe-akeneo",
Expand All @@ -35,7 +41,8 @@
"config": {
"allow-plugins": {
"composer/installers": false,
"silverstripe/vendor-plugin": false
"silverstripe/vendor-plugin": false,
"phpstan/extension-installer": true
}
}
}
26 changes: 26 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
parameters:
ignoreErrors:
-
message: "#^Call to method delete\\(\\) on an unknown class WeDevelop\\\\Akeneo\\\\Models\\\\ProductAssociation\\.ProductModel\\.$#"
count: 1
path: src/Imports/AkeneoImport.php

-
message: "#^Access to property \\$Type on an unknown class WeDevelop\\\\Akeneo\\\\Models\\\\ProductAssociation\\.Product\\.$#"
count: 2
path: src/Models/Product.php

-
message: "#^Call to method RelatedProduct\\(\\) on an unknown class WeDevelop\\\\Akeneo\\\\Models\\\\ProductAssociation\\.Product\\.$#"
count: 3
path: src/Models/Product.php

-
message: "#^Using nullsafe method call on non\\-nullable type WeDevelop\\\\Akeneo\\\\Models\\\\ProductMediaFile\\. Use \\-\\> instead\\.$#"
count: 1
path: src/Models/ProductAttributeValue.php

-
message: "#^Method WeDevelop\\\\Akeneo\\\\Service\\\\AkeneoApi\\:\\:refreshToken\\(\\) is unused\\.$#"
count: 1
path: src/Service/AkeneoApi.php
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 5
bootstrapFiles:
- vendor/syntro/silverstripe-phpstan/bootstrap.php
scanDirectories:
- src
paths:
- src

includes:
- vendor/syntro/silverstripe-phpstan/phpstan.neon
- phpstan-baseline.neon
24 changes: 24 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Netwerkstatt\SilverstripeRector\Set\SilverstripeSetList;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src'
]);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList:: CODING_STYLE,
SilverstripeSetList::CODE_STYLE,
]);

//$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon');
};
50 changes: 29 additions & 21 deletions src/Admins/AkeneoAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use WeDevelop\Akeneo\Imports\AkeneoImport;
use WeDevelop\Akeneo\Models\Display\DisplayGroup;
use WeDevelop\Akeneo\Models\Family;
use WeDevelop\Akeneo\Models\Product;
Expand All @@ -22,7 +22,10 @@

class AkeneoAdmin extends ModelAdmin
{
/** @config */
/**
* @config
* @var array<class-string>
*/
private static array $managed_models = [
Product::class,
ProductModel::class,
Expand All @@ -40,31 +43,34 @@ class AkeneoAdmin extends ModelAdmin
/** @config */
private static string $menu_icon = 'wedevelopnl/silverstripe-akeneo:images/akeneo.png';

public function getEditForm($id = null, $fields = null)
public function getEditForm($id = null, $fields = null): Form
{
$form = parent::getEditForm($id, $fields);

if ($this->modelClass === ProductCategory::class && $gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
if ($gridField instanceof GridField) {
$gridField->getConfig()->addComponent(new GridFieldOrderableRows('Sort'));
}
if ($this->modelClass === ProductCategory::class && ($gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) && $gridField instanceof GridField) {
$gridField->getConfig()->addComponent(GridFieldOrderableRows::create('Sort'));
}

if ($this->modelClass === DisplayGroup::class && $gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
if ($gridField instanceof GridField) {
$originalField = clone $gridField;

/** @var DataList $gridFieldList */
$gridFieldList = $gridField->getList();
/** @var DataList $originalFieldList */
$originalFieldList = $originalField->getList();

$originalField->setList(
$originalField->getList()->filter([
$originalFieldList->filter([
'IsRootGroup' => 0,
])
]),
);

$gridField->setName('RootDisplayGroups');
$gridField->setList(
$gridField->getList()->filter([
$gridFieldList->filter([
'IsRootGroup' => 1,
])
]),
);
}

Expand All @@ -76,10 +82,12 @@ public function getEditForm($id = null, $fields = null)
$gridField,
]);

$fields->addFieldsToTab('Root.Sub Groups', [
HeaderField::create('SubHeader', 'Sub groups are part of a group chain, and therefore have a parent group defined somewhere.'),
$originalField,
]);
if (!empty($originalField)) {
$fields->addFieldsToTab('Root.Sub Groups', [
HeaderField::create('SubHeader', 'Sub groups are part of a group chain, and therefore have a parent group defined somewhere.'),
$originalField,
]);
}
}

$importRunning = self::isImportRunning();
Expand All @@ -96,9 +104,9 @@ public function getEditForm($id = null, $fields = null)
public function doSync(): void
{
try {
$importMessage = self::asyncImport();
} catch (\Exception $e) {
$importMessage = $e->getMessage();
$importMessage = self::asyncImport();
} catch (\Exception $exception) {
$importMessage = $exception->getMessage();
}

Controller::curr()->getResponse()->addHeader('X-Status', $importMessage);
Expand All @@ -111,7 +119,7 @@ private static function isImportRunning(): bool
return (!empty($psOutput) && count($psOutput) > 2);
}

public static function asyncImport(): string
public static function asyncImport(): string
{
if (self::isImportRunning()) {
throw new \Exception('An import is still running.');
Expand All @@ -129,7 +137,7 @@ public function getCMSEditLink(DataObject $object, string $subTab = ''): string

$editFormField = 'EditForm/field/';

if ($subTab) {
if ($subTab !== '' && $subTab !== '0') {
$editFormField .= $subTab . '/';
}

Expand Down
2 changes: 2 additions & 0 deletions src/Controllers/ProductPageController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace WeDevelop\Akeneo\Controllers;

class ProductPageController extends \PageController
Expand Down
38 changes: 19 additions & 19 deletions src/Enums/ProductAttributeType.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

declare(strict_types=1);

namespace WeDevelop\Akeneo\Enums;

enum ProductAttributeType: string
{
case BOOLEAN = 'pim_catalog_boolean';
case DATE = 'pim_catalog_date';
case FILE = 'pim_catalog_file';
case IMAGE = 'pim_catalog_image';
case METRIC = 'pim_catalog_metric';
case MULTISELECT = 'pim_catalog_multiselect';
case PRICE_COLLECTION = 'pim_catalog_price_collection';
case SIMPLESELECT = 'pim_catalog_simpleselect';
case TEXT = 'pim_catalog_text';
case TEXTAREA = 'pim_catalog_textarea';
}
<?php

declare(strict_types=1);

namespace WeDevelop\Akeneo\Enums;

enum ProductAttributeType: string
{
case BOOLEAN = 'pim_catalog_boolean';
case DATE = 'pim_catalog_date';
case FILE = 'pim_catalog_file';
case IMAGE = 'pim_catalog_image';
case METRIC = 'pim_catalog_metric';
case MULTISELECT = 'pim_catalog_multiselect';
case PRICE_COLLECTION = 'pim_catalog_price_collection';
case SIMPLESELECT = 'pim_catalog_simpleselect';
case TEXT = 'pim_catalog_text';
case TEXTAREA = 'pim_catalog_textarea';
}
14 changes: 7 additions & 7 deletions src/Extensions/AkeneoSiteConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ public function updateCMSFields(FieldList $fields): void
*/
public function onBeforeWrite()
{
$this->owner->AkeneoURL = rtrim($this->owner->AkeneoURL ?? '', '/ ');
$this->getOwner()->AkeneoURL = rtrim($this->getOwner()->AkeneoURL ?? '', '/ ');
parent::onBeforeWrite();
}

private function credentialsExist(): bool
{
return $this->owner->AkeneoURL &&
$this->owner->AkeneoClientID &&
$this->owner->AkeneoSecret &&
$this->owner->AkeneoUsername &&
$this->owner->AkeneoPassword;
return $this->getOwner()->AkeneoURL &&
$this->getOwner()->AkeneoClientID &&
$this->getOwner()->AkeneoSecret &&
$this->getOwner()->AkeneoUsername &&
$this->getOwner()->AkeneoPassword;
}

private function canConnect(): bool
Expand All @@ -71,7 +71,7 @@ private function canConnect(): bool

try {
$this->akeneoApi->authorize();
} catch (ClientException $e) {
} catch (ClientException) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Extensions/AkeneoSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class AkeneoSiteTreeExtension extends DataExtension
{
public function augmentStageChildren(DataList &$staged): void
{
if ($this->owner->ClassName === SiteTree::class && $this->owner->ID === 0) {
if ($excludedPages = $this->owner->config()->get('excluded_root_pages')) {
$staged = $staged->Filter('ClassName:not', $excludedPages);
}
if ($this->getOwner()->ClassName === SiteTree::class && $this->getOwner()->ID === 0 && ($excludedPages = $this->getOwner()->config()->get('excluded_root_pages'))) {
$staged = $staged->Filter('ClassName:not', $excludedPages);
}
}
}
8 changes: 5 additions & 3 deletions src/Filters/TranslationLabelFilter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace WeDevelop\Akeneo\Filters;

use SilverStripe\ORM\DataQuery;
Expand All @@ -8,17 +10,17 @@

class TranslationLabelFilter extends SearchFilter
{
public function applyOne(DataQuery $query)
protected function applyOne(DataQuery $query): DataQuery
{
$value = $this->getValue();

$query = call_user_func([ProductAttribute::class, 'filterByLabel'], $query, $value);
$query = call_user_func(ProductAttribute::filterByLabel(...), $query, $value);

return $query;
}

protected function excludeOne(DataQuery $query)
{
// TODO: Implement excludeOne() method.
throw new \RuntimeException('Not implemented yet.');
}
}
Loading

0 comments on commit 8fd1902

Please sign in to comment.