Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oro/5.0.0 rc2 #21

Open
wants to merge 15 commits into
base: feature/update-5.0
Choose a base branch
from
Open
20 changes: 20 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: PHP Composer

on:
push:
branches: [ master 5.0 4.2 4.1 3.1 1.6 ]
pull_request:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install dependencies
run: php -r "copy('https://cs.symfony.com/download/php-cs-fixer-v3.phar', 'php-cs-fixer.phar');"

- name: Run php-cs-fixer
run: php php-cs-fixer.phar fix --dry-run --config=.php_cs.php --cache-file=.php_cs.cache --verbose --show-progress=dots --diff --allow-risky=yes
23 changes: 14 additions & 9 deletions .php_cs.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php

$finder = PhpCsFixer\Finder::create()
$finder = \PhpCsFixer\Finder::create()
->in(__DIR__);

return PhpCsFixer\Config::create()
return (new \PhpCsFixer\Config())
->setFinder($finder)
->setRules(
[
// generic PSRs
'@PSR1' => true,
'@PSR2' => true,
'psr0' => true,
'psr4' => true,
'@PSR12' => true,
'@PSR12:risky' => true,
'psr_autoloading' => true,

// imports
'ordered_imports' => true,
Expand Down Expand Up @@ -39,11 +40,14 @@
'escape_implicit_backslashes' => false,

// PHP
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHP74Migration' => true,
'@PHP74Migration:risky' => true,
'@PHP80Migration' => true,
'@PHP80Migration:risky' => true,

'use_arrow_functions' => false,
'get_class_to_class_keyword' => false,
'void_return' => false,
'visibility_required' => false,
'list_syntax' => ['syntax' => 'long'],
'declare_strict_types' => false,

Expand All @@ -64,11 +68,11 @@
'@Symfony:risky' => true,
'phpdoc_types_order' => false,
'phpdoc_separation' => false,
'phpdoc_inline_tag' => false,
'visibility_required' => ['elements' => ['property', 'method']],
'types_spaces' => false,
'native_function_invocation' => false,
'concat_space' => ['spacing' => 'one'],
'single_space_after_construct' => false,
'trailing_comma_in_multiline_array' => false,
'self_accessor' => false,
'yoda_style' => false,
'phpdoc_summary' => false,
Expand All @@ -87,6 +91,7 @@
'ternary_operator_spaces' => false,
'phpdoc_no_useless_inheritdoc' => false,
'class_definition' => false,
'string_length_to_empty' => false,
]
)
->setRiskyAllowed(true);
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

24 changes: 23 additions & 1 deletion Async/ImportProductProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Oro\Bundle\AkeneoBundle\Async;

use Doctrine\ORM\EntityManagerInterface;
use Oro\Bundle\AkeneoBundle\Tools\CacheProviderTrait;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\IntegrationBundle\Authentication\Token\IntegrationTokenAwareTrait;
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
use Oro\Bundle\IntegrationBundle\Entity\FieldsChanges;
use Oro\Bundle\IntegrationBundle\Provider\SyncProcessorRegistry;
use Oro\Bundle\MessageQueueBundle\Entity\Job;
use Oro\Component\MessageQueue\Client\TopicSubscriberInterface;
use Oro\Component\MessageQueue\Consumption\MessageProcessorInterface;
use Oro\Component\MessageQueue\Job\Job;
use Oro\Component\MessageQueue\Job\JobRunner;
use Oro\Component\MessageQueue\Transport\MessageInterface;
use Oro\Component\MessageQueue\Transport\SessionInterface;
Expand All @@ -19,6 +21,7 @@

class ImportProductProcessor implements MessageProcessorInterface, TopicSubscriberInterface
{
use CacheProviderTrait;
use IntegrationTokenAwareTrait;

/** @var DoctrineHelper */
Expand Down Expand Up @@ -97,12 +100,31 @@ public function process(MessageInterface $message, SessionInterface $session)
function (JobRunner $jobRunner, Job $child) use ($integration, $body) {
$this->doctrineHelper->refreshIncludingUnitializedRelations($integration);
$processor = $this->syncProcessorRegistry->getProcessorForIntegration($integration);

$em = $this->doctrineHelper->getEntityManager(FieldsChanges::class);
/** @var FieldsChanges $fieldsChanges */
$fieldsChanges = $em
->getRepository(FieldsChanges::class)
->findOneBy(['entityId' => $child->getId(), 'entityClass' => Job::class]);

if (!$fieldsChanges) {
$this->logger->error(
sprintf('Source data from Akeneo not found for job: %s', $child->getId())
);

return false;
}

$this->cacheProvider->save('akeneo', $fieldsChanges->getChangedFields());

$status = $processor->process(
$integration,
$body['connector'] ?? null,
$body['connector_parameters'] ?? []
);

$em->clear(FieldsChanges::class);

return $status;
}
);
Expand Down
6 changes: 3 additions & 3 deletions Client/AkeneoClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Oro\Bundle\AkeneoBundle\Client;

use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\PimEnterprise\ApiClient\AkeneoPimEnterpriseClientBuilder;
use Oro\Bundle\AkeneoBundle\Encoder\Crypter;
use Oro\Bundle\AkeneoBundle\Entity\AkeneoSettings;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
Expand Down Expand Up @@ -117,9 +117,9 @@ private function createClientByToken(): AkeneoPimClientInterface
return $this->client;
}

private function getClientBuilder(): AkeneoPimEnterpriseClientBuilder
private function getClientBuilder(): AkeneoPimClientBuilder
{
$clientBuilder = new AkeneoPimEnterpriseClientBuilder($this->akeneoUrl);
$clientBuilder = new AkeneoPimClientBuilder($this->akeneoUrl);
$clientBuilder->setHttpClient($this->httpClient);
$clientBuilder->setRequestFactory($this->requestFactory);
$clientBuilder->setStreamFactory($this->streamFactory);
Expand Down
104 changes: 104 additions & 0 deletions Command/CleanupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Oro\Bundle\AkeneoBundle\Command;

use Oro\Bundle\BatchBundle\ORM\Query\BufferedIdentityQueryResultIterator;
use Oro\Bundle\CronBundle\Command\CronCommandInterface;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\IntegrationBundle\Entity\FieldsChanges;
use Oro\Bundle\MessageQueueBundle\Entity\Job;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Clears old records from oro_integration_fields_changes table before repack.
*/
class CleanupCommand extends Command implements CronCommandInterface
{
/** @var string */
protected static $defaultName = 'oro:cron:akeneo:cleanup';

/** @var DoctrineHelper */
private $doctrineHelper;

public function __construct(DoctrineHelper $doctrineHelper)
{
$this->doctrineHelper = $doctrineHelper;
parent::__construct();
}

public function isActive()
{
return true;
}

public function getDefaultDefinition()
{
return '0 2 * * 6';
}

public function configure()
{
$this
->setDescription('Clears old records from oro_integration_fields_changes table.')
->setHelp(
<<<'HELP'
The <info>%command.name%</info> command clears fields changes for complete job records
from <comment>oro_integration_fields_changes</comment> table.

<info>php %command.full_name%</info>
HELP
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf(
'<comment>Number of fields changes that has been deleted:</comment> %d',
$this->deleteRecords()
));

$output->writeln('<info>Fields changes cleanup complete</info>');
}

private function deleteRecords(): int
{
$qb = $this->doctrineHelper
->getEntityManagerForClass(FieldsChanges::class)
->getRepository(FieldsChanges::class)
->createQueryBuilder('fc');

$qb
->delete(FieldsChanges::class, 'fc')
->where($qb->expr()->eq('fc.entityClass', ':class'))
->setParameter('class', Job::class)
->andWhere($qb->expr()->in('fc.entityId', ':ids'));

$jqb = $this->doctrineHelper
->getEntityManagerForClass(Job::class)
->getRepository(Job::class)
->createQueryBuilder('j');

$jqb
->select('j.id')
->where($jqb->expr()->in('j.status', ':statuses'))
->setParameter('statuses', [Job::STATUS_SUCCESS, Job::STATUS_CANCELLED, Job::STATUS_FAILED, Job::STATUS_STALE])
->orderBy($jqb->expr()->desc('j.id'));

$iterator = new BufferedIdentityQueryResultIterator($jqb->getQuery());

$result = 0;
$iterator->setPageLoadedCallback(function (array $rows) use ($qb, &$result): array {
$ids = array_column($rows, 'id');

$result = $result + $qb->setParameter('ids', $ids)->getQuery()->execute();

return $ids;
});

iterator_to_array($iterator);

return $result;
}
}
50 changes: 35 additions & 15 deletions Controller/ValidateConnectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,43 @@

use Akeneo\Pim\ApiClient\Exception\ExceptionInterface;
use Oro\Bundle\AkeneoBundle\Entity\AkeneoSettings;
use Oro\Bundle\AkeneoBundle\Integration\AkeneoTransportInterface;
use Oro\Bundle\CurrencyBundle\Provider\CurrencyProviderInterface;
use Oro\Bundle\IntegrationBundle\Entity\Channel;
use Oro\Bundle\IntegrationBundle\Form\Type\ChannelType;
use Oro\Bundle\SecurityBundle\Annotation\Acl;
use Psr\Http\Client\ClientExceptionInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;

class ValidateConnectionController extends AbstractController
{
const CONNECTION_SUCCESSFUL_MESSAGE = 'oro.akeneo.connection.successfull';
const CONNECTION_ERROR_MESSAGE = 'oro.akeneo.connection.error';

/** @var CurrencyProviderInterface */
private $currencyProvider;

/** @var TranslatorInterface */
private $translator;

/** @var AkeneoTransportInterface */
private $akeneoTransport;

public function __construct(
CurrencyProviderInterface $currencyProvider,
TranslatorInterface $translator,
AkeneoTransportInterface $akeneoTransport
) {
$this->currencyProvider = $currencyProvider;
$this->translator = $translator;
$this->akeneoTransport = $akeneoTransport;
}

/**
* @Route(path="/validate-akeneo-connection/{channelId}/", name="oro_akeneo_validate_connection", methods={"POST"})
* @ParamConverter("channel", class="OroIntegrationBundle:Channel", options={"id"="channelId"})
Expand Down Expand Up @@ -52,38 +75,35 @@ public function validateConnectionAction(Request $request, Channel $channel = nu
$akeneoSettings->setPassword($akeneoSettingsEntity->getPassword());
}

$currencyConfig = $this->container->get('oro_currency.config.currency');

$akeneoChannelNames = [];
$akeneoCurrencies = [];
$akeneoLocales = [];

try {
$transport = $this->get('oro_akeneo.integration.transport');
$transport->init($akeneoSettings, false);
$this->akeneoTransport->init($akeneoSettings, false);
$success = true;
$message = self::CONNECTION_SUCCESSFUL_MESSAGE;
$message = $this->translator->trans(self::CONNECTION_SUCCESSFUL_MESSAGE);
switch ($request->get('synctype', 'all')) {
case 'channels':
$akeneoChannelNames = $transport->getChannels();
$akeneoChannelNames = $this->akeneoTransport->getChannels();
break;
case 'currencies':
$akeneoCurrencies = $transport->getMergedCurrencies();
$akeneoCurrencies = $this->akeneoTransport->getMergedCurrencies();
break;
case 'locales':
$akeneoLocales = $transport->getLocales();
$akeneoLocales = $this->akeneoTransport->getLocales();
break;
default:
$akeneoChannelNames = $transport->getChannels();
$akeneoCurrencies = $transport->getMergedCurrencies();
$akeneoLocales = $transport->getLocales();
$akeneoChannelNames = $this->akeneoTransport->getChannels();
$akeneoCurrencies = $this->akeneoTransport->getMergedCurrencies();
$akeneoLocales = $this->akeneoTransport->getLocales();
}
} catch (ExceptionInterface $e) {
} catch (ClientExceptionInterface | ExceptionInterface $e) {
$success = false;
$message = $e->getMessage();
} catch (\Exception $e) {
$success = false;
$message = self::CONNECTION_ERROR_MESSAGE;
$message = $this->translator->trans(self::CONNECTION_ERROR_MESSAGE);
}

return new JsonResponse(
Expand All @@ -92,8 +112,8 @@ public function validateConnectionAction(Request $request, Channel $channel = nu
'akeneoCurrencies' => $akeneoCurrencies,
'akeneoLocales' => $akeneoLocales,
'success' => $success,
'message' => $this->get('translator')->trans($message),
'currencyList' => $currencyConfig->getCurrencies(),
'message' => $message,
'currencyList' => $this->currencyProvider->getCurrencyList(),
]
);
}
Expand Down
Loading