EzPlatformAdapter::executeAction()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 5
rs 8.8571
1
<?php
2
3
/**
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\EzPlatform\Adapter;
11
12
use eZ\Publish\API\Repository\Repository;
13
use Psr\Log\LoggerAwareInterface;
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Transfer\Adapter\TargetAdapterInterface;
17
use Transfer\Adapter\Transaction\Request;
18
use Transfer\Adapter\Transaction\Response;
19
use Transfer\Data\ObjectInterface;
20
use Transfer\Data\TreeObject;
21
use Transfer\EzPlatform\Repository\Values\Action\Enum\Action;
22
use Transfer\EzPlatform\Repository\Manager\Core\AbstractRepositoryService;
23
use Transfer\EzPlatform\Repository\Manager\Core\ContentTreeService;
24
use Transfer\EzPlatform\Repository\Manager\Core\ObjectService;
25
use Transfer\EzPlatform\Repository\Values\EzPlatformObject;
26
27
/**
28
 * eZ Platform adapter.
29
 */
30
class EzPlatformAdapter implements TargetAdapterInterface, LoggerAwareInterface
31
{
32
    /**
33
     * @var array Options
34
     */
35
    protected $options;
36
37
    /**
38
     * @var LoggerInterface Logger
39
     */
40
    protected $logger;
41
42
    /**
43
     * @var Repository
44
     */
45
    protected $repository;
46
47
    /**
48
     * @var ContentTreeService Tree service
49
     */
50
    protected $treeService;
51
52
    /**
53
     * @var ObjectService Object service
54
     */
55
    protected $objectService;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param Repository $repository
61
     * @param array      $options
62 74
     */
63
    public function __construct(Repository $repository, array $options = array())
64 74
    {
65
        $this->repository = $repository;
66 74
67 74
        $resolver = new OptionsResolver();
68
        $this->configureOptions($resolver);
69 74
70
        $this->options = $resolver->resolve($options);
71 74
72 74
        $this->objectService = new ObjectService($repository, $this->options);
73 74
        $this->treeService = new ContentTreeService($repository, $this->options, $this->objectService);
74
    }
75
76
    /**
77
     * Option configuration.
78
     *
79
     * @param OptionsResolver $resolver
80 74
     */
81
    protected function configureOptions(OptionsResolver $resolver)
82 74
    {
83 74
        $resolver->setDefaults(array(
84 74
            'current_user' => 'admin',
85 74
            'main_language_code' => 'eng-GB',
86
        ));
87 74
88 74
        $resolver->setAllowedTypes('current_user', array('string', 'null'));
89 74
        $resolver->setAllowedTypes('main_language_code', array('string', 'null'));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94 73
     */
95
    public function setLogger(LoggerInterface $logger)
96 73
    {
97 73
        $this->logger = $logger;
98 73
        $this->objectService->setLogger($logger);
99 73
        $this->treeService->setLogger($logger);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104 34
     */
105
    public function send(Request $request)
106 34
    {
107
        $this->repository->beginTransaction();
108 34
109 33
        if ($this->logger) {
110 33
            $this->treeService->setLogger($this->logger);
111 33
            $this->objectService->setLogger($this->logger);
112
        }
113 34
114
        $response = new Response();
115 34
116 34
        $objects = array();
117 34
        foreach ($request as $object) {
118
            $service = $this->getService($object);
119
120 34
            try {
121 34
                $objects[] = $this->executeAction($object, $service);
122 1
            } catch (\Exception $e) {
123 1
                $this->repository->rollback();
124
                throw $e;
125
            }
126 33
127 33
            if (!empty($objects)) {
128 33
                $response->setData(new \ArrayIterator($objects));
129 33
            }
130
        }
131 33
132
        $this->repository->commit();
133 33
134
        return $response;
135
    }
136
137
    /**
138
     * @param ObjectInterface           $object
139
     * @param AbstractRepositoryService $service
140
     *
141
     * @return ObjectInterface|null
142 34
     */
143
    protected function executeAction(ObjectInterface $object, AbstractRepositoryService $service)
144 34
    {
145
        if (is_a($object, EzPlatformObject::class)) {
146 31
            /** @var EzPlatformObject $object */
147 31
            switch ($object->getAction()) {
148 26
                case Action::CREATEORUPDATE:
149 7
                    return $service->createOrUpdate($object);
150 6
                case Action::DELETE:
151 1
                    return $service->remove($object);
152 1
                case Action::SKIP:
153 1
                default:
154 1
            }
155 3
        } else {
156
            return $service->createOrUpdate($object);
157
        }
158 1
159
        return;
160
    }
161
162
    /**
163
     * Decides which service to use, based on the type of $object given.
164
     *
165
     * @param ObjectInterface $object
166
     *
167
     * @return ContentTreeService|ObjectService
168 34
     */
169
    protected function getService($object)
170 34
    {
171 2
        if ($object instanceof TreeObject) {
172 2
            $service = $this->treeService;
173 32
        } else {
174
            $service = $this->objectService;
175
        }
176 34
177 34
        if ($this->options['current_user']) {
178 34
            $service->setCurrentUser($this->options['current_user']);
179
        }
180 34
181
        return $service;
182
    }
183
}
184