1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\AppBundle\Service\Locale\Copier; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Doctrine\Common\Collections\Criteria; |
17
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
18
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\Locale; |
19
|
|
|
use WellCommerce\Bundle\CoreBundle\Entity\LocaleAwareInterface; |
20
|
|
|
use WellCommerce\Bundle\CoreBundle\Helper\Doctrine\DoctrineHelperInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class LocaleCopier |
24
|
|
|
* |
25
|
|
|
* @author Adam Piotrowski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class LocaleCopier implements LocaleCopierInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $entityClasses; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var DoctrineHelperInterface |
36
|
|
|
*/ |
37
|
|
|
protected $doctrineHelper; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Symfony\Component\PropertyAccess\PropertyAccessor |
41
|
|
|
*/ |
42
|
|
|
protected $propertyAccessor; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* LocaleCopier constructor. |
46
|
|
|
* |
47
|
|
|
* @param array $entityClasses |
48
|
|
|
* @param DoctrineHelperInterface $doctrineHelper |
49
|
|
|
*/ |
50
|
|
|
public function __construct(array $entityClasses, DoctrineHelperInterface $doctrineHelper) |
51
|
|
|
{ |
52
|
|
|
$this->entityClasses = $entityClasses; |
53
|
|
|
$this->doctrineHelper = $doctrineHelper; |
54
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function copyLocaleData(Locale $sourceLocale, Locale $targetLocale) |
58
|
|
|
{ |
59
|
|
|
$criteria = new Criteria(); |
60
|
|
|
$criteria->where($criteria->expr()->eq('locale', $sourceLocale->getCode())); |
61
|
|
|
|
62
|
|
|
foreach ($this->entityClasses as $className => $options) { |
63
|
|
|
$repository = $this->doctrineHelper->getEntityManager()->getRepository($className); |
64
|
|
|
$entities = $repository->matching($criteria); |
65
|
|
|
$this->duplicateTranslatableEntities($entities, $options['properties'], $targetLocale); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->doctrineHelper->getEntityManager()->flush(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function duplicateTranslatableEntities(Collection $entities, array $properties, Locale $targetLocale) |
72
|
|
|
{ |
73
|
|
|
$entities->map(function (LocaleAwareInterface $entity) use ($properties, $targetLocale) { |
74
|
|
|
$this->duplicateTranslatableEntity($entity, $properties, $targetLocale); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function duplicateTranslatableEntity(LocaleAwareInterface $entity, array $properties, Locale $targetLocale) |
79
|
|
|
{ |
80
|
|
|
$duplicate = clone $entity; |
81
|
|
|
foreach ($properties as $propertyName) { |
82
|
|
|
$value = sprintf('%s-%s', $this->propertyAccessor->getValue($entity, $propertyName), $targetLocale->getCode()); |
83
|
|
|
$this->propertyAccessor->setValue($duplicate, $propertyName, $value); |
84
|
|
|
$duplicate->setLocale($targetLocale->getCode()); |
85
|
|
|
$this->doctrineHelper->getEntityManager()->persist($duplicate); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.