These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace HDNET\Importr\Service\Targets; |
||
3 | |||
4 | use HDNET\Importr\Domain\Model\Strategy; |
||
5 | use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; |
||
6 | use TYPO3\CMS\Extbase\Object\ObjectManagerInterface; |
||
7 | use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface; |
||
8 | |||
9 | /** |
||
10 | * Description of ExtbaseModel |
||
11 | * |
||
12 | * @author tim |
||
13 | */ |
||
14 | class ExtbaseModel extends AbstractTarget implements TargetInterface |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var Strategy |
||
19 | */ |
||
20 | protected $strategy; |
||
21 | |||
22 | /** |
||
23 | * @var \TYPO3\CMS\Extbase\Persistence\RepositoryInterface |
||
24 | */ |
||
25 | protected $repository; |
||
26 | |||
27 | /** |
||
28 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
29 | */ |
||
30 | protected $persistenceManager; |
||
31 | |||
32 | /** |
||
33 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
34 | */ |
||
35 | protected $objectManager; |
||
36 | |||
37 | /** |
||
38 | * ExtbaseModel constructor. |
||
39 | * |
||
40 | * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager |
||
41 | * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager |
||
42 | */ |
||
43 | public function __construct(PersistenceManagerInterface $persistenceManager, ObjectManagerInterface $objectManager) |
||
44 | { |
||
45 | $this->persistenceManager = $persistenceManager; |
||
46 | $this->objectManager = $objectManager; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return array |
||
51 | */ |
||
52 | public function getConfiguration() |
||
53 | { |
||
54 | $configuration = parent::getConfiguration(); |
||
55 | View Code Duplication | if (!isset($configuration['pid']) || !is_numeric($configuration['pid'])) { |
|
0 ignored issues
–
show
|
|||
56 | $configuration['pid'] = 0; |
||
57 | } |
||
58 | |||
59 | return $configuration; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param \HDNET\Importr\Domain\Model\Strategy $strategy |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | public function start(Strategy $strategy) |
||
68 | { |
||
69 | $this->strategy = $strategy; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param array $entry |
||
74 | * |
||
75 | * @return int|void |
||
76 | */ |
||
77 | public function processEntry(array $entry) |
||
78 | { |
||
79 | $configuration = $this->getConfiguration(); |
||
80 | $this->repository = $this->objectManager->get($configuration['repository']); |
||
81 | |||
82 | $model = $this->mapModel($this->getModel(), $configuration['mapping'], $entry); |
||
83 | $this->repository->add($model); |
||
84 | $this->persistenceManager->persistAll(); |
||
85 | |||
86 | if (isset($configuration['language']) && is_array($configuration['language'])) { |
||
87 | $this->processLanguageEntries($configuration['language'], $model, $entry); |
||
88 | } |
||
89 | $this->persistenceManager->persistAll(); |
||
90 | |||
91 | return TargetInterface::RESULT_INSERT; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param array $configuration |
||
96 | * @param AbstractEntity $model |
||
97 | * @param $entry |
||
98 | */ |
||
99 | protected function processLanguageEntries(array $configuration, $model, $entry) |
||
100 | { |
||
101 | foreach ($configuration as $languageKey => $mapping) { |
||
102 | $modelLang = $this->mapModel($this->getModel(), $mapping, $entry); |
||
103 | |||
104 | if (method_exists($modelLang, 'setSysLanguageUid') && method_exists($modelLang, 'setL10nParent')) { |
||
105 | $modelLang->setSysLanguageUid($languageKey); |
||
106 | $modelLang->setL10nParent($model); |
||
107 | |||
108 | $this->repository->add($modelLang); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | public function end() |
||
114 | { |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $model |
||
119 | * @param array $mapping |
||
120 | * @param $entry |
||
121 | * |
||
122 | * @return \TYPO3\CMS\Extbase\DomainObject\AbstractEntity |
||
123 | */ |
||
124 | protected function mapModel($model, $mapping, $entry) |
||
125 | { |
||
126 | if (is_array($mapping)) { |
||
127 | foreach ($mapping as $key => $value) { |
||
128 | $model->_setProperty($value, $entry[$key]); |
||
129 | } |
||
130 | } |
||
131 | return $model; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * get a model in the right location |
||
136 | * |
||
137 | * @return \TYPO3\CMS\Extbase\DomainObject\AbstractEntity |
||
138 | */ |
||
139 | protected function getModel() |
||
140 | { |
||
141 | $configuration = $this->getConfiguration(); |
||
142 | /** |
||
143 | * @var \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $model |
||
144 | */ |
||
145 | $model = new $configuration['model']; |
||
146 | $model->setPid($configuration['pid']); |
||
147 | return $model; |
||
148 | } |
||
149 | } |
||
150 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.