|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Bundle\Core\Command; |
| 10 | + |
| 11 | +use Doctrine\DBAL\Connection; |
| 12 | +use RuntimeException; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Input\InputOption; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | + |
| 18 | +final class DuplicateFieldsCommand extends Command |
| 19 | +{ |
| 20 | + /** @var \Doctrine\DBAL\Connection */ |
| 21 | + private $connection; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + Connection $connection |
| 25 | + ) { |
| 26 | + parent::__construct('ibexa:content:duplicate-fields'); |
| 27 | + |
| 28 | + $this->connection = $connection; |
| 29 | + } |
| 30 | + |
| 31 | + public function configure(): void |
| 32 | + { |
| 33 | + $this->addOption('count', null, InputOption::VALUE_REQUIRED); |
| 34 | + $this->addOption('version-no', null, InputOption::VALUE_REQUIRED); |
| 35 | + $this->addOption('attribute-id', null, InputOption::VALUE_REQUIRED); |
| 36 | + $this->addOption('content-id', null, InputOption::VALUE_REQUIRED); |
| 37 | + $this->addOption('language-id', null, InputOption::VALUE_REQUIRED); |
| 38 | + } |
| 39 | + |
| 40 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 41 | + { |
| 42 | + $query = $this->connection->createQueryBuilder(); |
| 43 | + |
| 44 | + $query |
| 45 | + ->select('*') |
| 46 | + ->from('ezcontentobject_attribute') |
| 47 | + ->andwhere('version = :version') |
| 48 | + ->andWhere('contentobject_id = :content_id') |
| 49 | + ->andWhere('contentclassattribute_id = :attribute_id') |
| 50 | + ->andWhere('language_id = :language_id') |
| 51 | + ->orderBy('id', 'desc') |
| 52 | + ->setMaxResults(1); |
| 53 | + |
| 54 | + $attribute = $query->setParameters([ |
| 55 | + 'version' => (int)$input->getOption('version-no'), |
| 56 | + 'content_id' => (int)$input->getOption('content-id'), |
| 57 | + 'attribute_id' => (int)$input->getOption('attribute-id'), |
| 58 | + 'language_id' => (int)$input->getOption('language-id'), |
| 59 | + ])->execute()->fetchAssociative(); |
| 60 | + |
| 61 | + if (false === $attribute) { |
| 62 | + throw new RuntimeException('Attribute not found'); |
| 63 | + } |
| 64 | + |
| 65 | + unset($attribute['id']); |
| 66 | + |
| 67 | + $count = (int)$input->getOption('count'); |
| 68 | + |
| 69 | + $query = $this->connection->createQueryBuilder(); |
| 70 | + $query |
| 71 | + ->insert('ezcontentobject_attribute'); |
| 72 | + |
| 73 | + foreach ($attribute as $key => $value) { |
| 74 | + $query->setValue($key, ':' . $key); |
| 75 | + $query->setParameter(':' . $key, $value); |
| 76 | + } |
| 77 | + |
| 78 | + for ($i = 0; $i < $count; ++$i) { |
| 79 | + $query->execute(); |
| 80 | + } |
| 81 | + |
| 82 | + return Command::SUCCESS; |
| 83 | + } |
| 84 | +} |
0 commit comments