Completed
Pull Request — 1.0 (#53)
by Harald
08:20 queued 01:22
created

EzPlatformAdapter::executeAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 8.6737
cc 5
eloc 15
nc 5
nop 2
crap 5
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 ReflectionClass;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
use Transfer\Adapter\TargetAdapterInterface;
18
use Transfer\Adapter\Transaction\Request;
19
use Transfer\Adapter\Transaction\Response;
20
use Transfer\Data\ObjectInterface;
21
use Transfer\Data\TreeObject;
22
use Transfer\EzPlatform\Repository\Values\Action\ActionInterface;
23
use Transfer\EzPlatform\Repository\Values\Action\Enum\Action;
24
use Transfer\EzPlatform\Repository\Manager\Core\AbstractRepositoryService;
25
use Transfer\EzPlatform\Repository\Manager\Core\ContentTreeService;
26
use Transfer\EzPlatform\Repository\Manager\Core\ObjectService;
27
28
/**
29
 * eZ Platform adapter.
30
 */
31
class EzPlatformAdapter implements TargetAdapterInterface, LoggerAwareInterface
32
{
33
    /**
34
     * @var ContentTreeService Tree service
35
     */
36
    protected $treeService;
37
38
    /**
39
     * @var ObjectService Object service
40
     */
41
    protected $objectService;
42
43
    /**
44
     * @var LoggerInterface Logger
45
     */
46
    protected $logger;
47
48
    /**
49
     * @var array Options
50
     */
51
    protected $options;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param array $options
57
     */
58 64
    public function __construct(array $options = array())
59
    {
60 64
        $resolver = new OptionsResolver();
61 64
        $this->configureOptions($resolver);
62
63 64
        $this->options = $resolver->resolve($options);
64
65 64
        $this->objectService = new ObjectService($this->options['repository']);
66 64
        $this->treeService = new ContentTreeService($this->options['repository'], $this->objectService);
67 64
    }
68
69
    /**
70
     * Option configuration.
71
     *
72
     * @param OptionsResolver $resolver
73
     */
74 64
    protected function configureOptions(OptionsResolver $resolver)
75
    {
76 64
        $resolver->setDefaults(array(
77 64
            'repository_current_user' => 'admin',
78 64
        ));
79
80 64
        $resolver->setRequired(array('repository'));
81
82 64
        $resolver->setAllowedTypes('repository', array('eZ\Publish\API\Repository\Repository'));
83 64
        $resolver->setAllowedTypes('repository_current_user', array('string', 'null'));
84 64
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 64
    public function setLogger(LoggerInterface $logger)
90
    {
91 64
        $this->logger = $logger;
92 64
        $this->objectService->setLogger($logger);
93 64
        $this->treeService->setLogger($logger);
94 64
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 25
    public function send(Request $request)
100
    {
101
        /** @var Repository $repository */
102 25
        $repository = $this->options['repository'];
103 25
        $repository->beginTransaction();
104
105 25
        if ($this->logger) {
106 25
            $this->treeService->setLogger($this->logger);
107 25
            $this->objectService->setLogger($this->logger);
108 25
        }
109
110 25
        $response = new Response();
111
112 25
        $objects = array();
113 25
        foreach ($request as $object) {
114 25
            $service = $this->getService($object);
115
116
            try {
117 25
                $objects[] = $this->executeAction($object, $service);
118 25
            } catch (\Exception $e) {
119 2
                $repository->rollback();
120 2
                throw $e;
121
            }
122
123 23
            if (!empty($objects)) {
124 23
                $response->setData(new \ArrayIterator($objects));
125 23
            }
126 23
        }
127
128 23
        $repository->commit();
129
130 23
        return $response;
131
    }
132
133
    /**
134
     * @param ObjectInterface           $object
135
     * @param AbstractRepositoryService $service
136
     *
137
     * @return ObjectInterface|null
138
     */
139 25
    protected function executeAction(ObjectInterface $object, AbstractRepositoryService $service)
140
    {
141 25
        $reflection = new ReflectionClass($object);
142
143 25
        if ($reflection->implementsInterface(ActionInterface::class)) {
144
            /** @var ActionInterface $object */
145 22
            switch ($object->getAction()) {
146 22
                case Action::CREATEORUPDATE:
147 17
                    return $service->createOrUpdate($object);
0 ignored issues
show
Documentation introduced by
$object is of type object<Transfer\EzPlatfo...Action\ActionInterface>, but the function expects a object<Transfer\Data\ObjectInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
149 7
                case Action::DELETE:
150 6
                    return $service->remove($object);
0 ignored issues
show
Documentation introduced by
$object is of type object<Transfer\EzPlatfo...Action\ActionInterface>, but the function expects a object<Transfer\Data\ObjectInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
152 1
                case Action::SKIP:
153 1
                default:
154 1
            }
155 1
        } else {
156 3
            return $service->createOrUpdate($object);
157
        }
158
159 1
        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
     */
169 25
    protected function getService($object)
170
    {
171 25
        if ($object instanceof TreeObject) {
172 2
            $service = $this->treeService;
173 2
        } else {
174 23
            $service = $this->objectService;
175
        }
176
177 25
        if ($this->options['repository_current_user']) {
178 25
            $service->setCurrentUser($this->options['repository_current_user']);
179 25
        }
180
181 25
        return $service;
182
    }
183
}
184