GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — dev ( cafe43...4095d1 )
by Андрей
04:03
created

DoctrineWorkflowStoryService   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
c 3
b 0
f 0
lcom 1
cbo 11
dl 0
loc 272
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A init() 0 6 1
B bindObjectToWorkflowEntry() 0 31 1
B restoreObjectBindingToEntry() 0 31 4
A getSerializerManager() 0 4 1
A setSerializerManager() 0 6 1
A getSerializerName() 0 4 1
A setSerializerName() 0 6 1
A getModuleOptions() 0 4 1
A setModuleOptions() 0 6 1
A getWorkflowService() 0 4 1
A setWorkflowService() 0 6 1
A getEntryId() 0 3 1
1
<?php
2
/**
3
 * @link    https://github.com/old-town/workflow-zf2-toolkit
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\ZF2\Toolkit\DoctrineWorkflowStory;
7
8
use Zend\Serializer\AdapterPluginManager as SerializerManager;
9
use OldTown\Workflow\ZF2\Service\Annotation as WFS;
10
use Zend\Serializer\Adapter\AdapterInterface as Serializer;
11
use OldTown\Workflow\ZF2\Toolkit\Entity\DoctrineWorkflowStory\ExtEntry;
12
use OldTown\Workflow\ZF2\Toolkit\Entity\DoctrineWorkflowStory\ObjectInfo;
13
use OldTown\Workflow\ZF2\Toolkit\Options\ModuleOptions;
14
use ReflectionClass;
15
use OldTown\Workflow\ZF2\ServiceEngine\WorkflowServiceInterface;
16
17
/**
18
 * Class DoctrineWorkflowStoryService
19
 *
20
 * @package OldTown\Workflow\ZF2\Toolkit\DoctrineWorkflowStory
21
 */
22
class DoctrineWorkflowStoryService
23
{
24
    /**
25
     * Псевдоним для объектов по умолчанию
26
     *
27
     * @var string
28
     */
29
    const DEFAULT_OBJECT = 'defaultObject';
30
31
    /**
32
     * Менеджер для получения адапторов отвечающих за сериализацию данных
33
     *
34
     * @var SerializerManager
35
     */
36
    protected $serializerManager;
37
38
    /**
39
     * Сериалайзер по умолчанию
40
     *
41
     * @var string
42
     */
43
    protected $serializerName = 'json';
44
45
    /**
46
     * Настройки модуля
47
     *
48
     * @var ModuleOptions
49
     */
50
    protected $moduleOptions;
51
52
    /**
53
     * Сервис для работы с workflow
54
     *
55
     * @var WorkflowServiceInterface
56
     */
57
    protected $workflowService;
58
59
    /**
60
     * DoctrineWorkflowStoryService constructor.
61
     *
62
     * @param array $options
63
     */
64
    public function __construct(array $options = [])
65
    {
66
        $initOptions = [
67
            array_key_exists('serializerManager', $options) ? $options['serializerManager'] : null,
68
            array_key_exists('moduleOptions', $options) ? $options['moduleOptions'] : null,
69
            array_key_exists('workflowService', $options) ? $options['workflowService'] : null
70
        ];
71
        call_user_func_array([$this, 'init'], $initOptions);
72
    }
73
74
    /**
75
     * @param SerializerManager        $serializerManager
76
     * @param ModuleOptions            $moduleOptions
77
     * @param WorkflowServiceInterface $workflowService
78
     */
79
    protected function init(SerializerManager $serializerManager, ModuleOptions $moduleOptions, WorkflowServiceInterface $workflowService)
80
    {
81
        $this->setSerializerManager($serializerManager);
82
        $this->setModuleOptions($moduleOptions);
83
        $this->setWorkflowService($workflowService);
84
    }
85
86
    /**
87
     * @WFS\ArgumentsMap(argumentsMap={
88
     *      @WFS\Map(fromArgName="entryParam", to="entry"),
89
     *      @WFS\Map(fromArgName="objectParam", to="object"),
90
     *      @WFS\Map(fromArgName="objectAliasParam", to="objectAlias"),
91
     *      @WFS\Map(fromArgName="storeParam", to="store")
92
     *})
93
     *
94
     *
95
     * @param ExtEntry               $entry
96
     * @param mixed                  $object
97
     * @param string                 $objectAlias
98
     * @param DoctrineWorkflowStory $store
99
     *
100
     * @throws Exception\InvalidWorkflowStoreException
101
     * @throws \OldTown\Workflow\Spi\Doctrine\Exception\DoctrineRuntimeException
102
     * @throws Exception\RuntimeException
103
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
104
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
105
     * @throws \Zend\ServiceManager\Exception\RuntimeException
106
     * @throws \Zend\Serializer\Exception\ExceptionInterface
107
     */
108
    public function bindObjectToWorkflowEntry(ExtEntry $entry, $object, $objectAlias = self::DEFAULT_OBJECT, DoctrineWorkflowStory $store)
109
    {
110
        $em = $store->getEntityManager();
111
112
        $objectClass = get_class($object);
113
        $metadata = $em->getClassMetadata($objectClass);
114
115
        $serializerName = $this->getSerializerName();
116
        /** @var Serializer $serializer */
117
        $serializer = $this->getSerializerManager()->get($serializerName);
118
119
        $id = $metadata->getIdentifierValues($object);
120
        $serializedId = $serializer->serialize($id);
121
122
        $objectInfoClass = $this->getModuleOptions()->getEntityClassName('DoctrineWorkflowStory\\ObjectInfo');
123
124
        $r = new ReflectionClass($objectInfoClass);
125
        /** @var  ObjectInfo $objectInfo */
126
        $objectInfo = $r->newInstance();
127
128
        $objectInfo->setClassName($objectClass);
129
        $objectInfo->setObjectId($serializedId);
130
        $objectInfo->setAlias($objectAlias);
131
        $objectInfo->addEntry($entry);
132
133
134
        $entry->addObjectInfo($objectInfo);
135
136
        $em->persist($objectInfo);
137
        $em->flush();
138
    }
139
140
    /**
141
     * Востановить объект привязанный к процессу
142
     *
143
     * @WFS\ArgumentsMap(argumentsMap={
144
     *      @WFS\Map(fromArgName="entryParam", to="entry"),
145
     *      @WFS\Map(fromArgName="objectAliasParam", to="objectAlias"),
146
     *      @WFS\Map(fromArgName="storeParam", to="store")
147
     * })
148
     *
149
     * @WFS\ResultVariable(name="resultVariableName")
150
     *
151
     *
152
     * @param ExtEntry              $entry
153
     * @param string                $objectAlias
154
     * @param DoctrineWorkflowStory $store
155
     *
156
     * @return mixed
157
     *
158
     * @throws \OldTown\Workflow\Spi\Doctrine\Exception\DoctrineRuntimeException
159
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
160
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
161
     * @throws \Zend\ServiceManager\Exception\RuntimeException
162
     * @throws \Zend\Serializer\Exception\ExceptionInterface
163
     * @throws Exception\InvalidRestoreObjectException
164
     * @throws Exception\InvalidArgumentException
165
     */
166
    public function restoreObjectBindingToEntry(ExtEntry $entry, $objectAlias = self::DEFAULT_OBJECT, DoctrineWorkflowStory $store)
167
    {
168
        $objectsInfo = $entry->getObjectsInfo();
169
170
        foreach ($objectsInfo as $objectInfo) {
171
            if ($objectAlias === $objectInfo->getAlias()) {
172
                $className = $objectInfo->getClassName();
173
174
                $em = $store->getEntityManager();
175
176
                $serializerName = $this->getSerializerName();
177
                /** @var Serializer $serializer */
178
                $serializer = $this->getSerializerManager()->get($serializerName);
179
180
                $serializedId = $objectInfo->getObjectId();
181
                $id = $serializer->unserialize($serializedId);
182
183
                $object = $em->getRepository($className)->find($id);
184
185
                if (!is_object($object)) {
186
                    $errMsg = sprintf('Invalid restore object. Alias: %s. Class: %s. Id: %s', $objectAlias, $className, $serializedId);
187
                    throw new Exception\InvalidRestoreObjectException($errMsg);
188
                }
189
190
                return $object;
191
            }
192
        }
193
194
        $errMsg = sprintf('Invalid object alias: %s', $objectAlias);
195
        throw new Exception\InvalidArgumentException($errMsg);
196
    }
197
198
    /**
199
     * @return SerializerManager
200
     */
201
    public function getSerializerManager()
202
    {
203
        return $this->serializerManager;
204
    }
205
206
    /**
207
     * @param SerializerManager $serializerManager
208
     *
209
     * @return $this
210
     */
211
    public function setSerializerManager(SerializerManager $serializerManager)
212
    {
213
        $this->serializerManager = $serializerManager;
214
215
        return $this;
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function getSerializerName()
222
    {
223
        return $this->serializerName;
224
    }
225
226
    /**
227
     * @param string $serializerName
228
     *
229
     * @return $this
230
     */
231
    public function setSerializerName($serializerName)
232
    {
233
        $this->serializerName = $serializerName;
234
235
        return $this;
236
    }
237
238
    /**
239
     * @return ModuleOptions
240
     */
241
    public function getModuleOptions()
242
    {
243
        return $this->moduleOptions;
244
    }
245
246
    /**
247
     * @param ModuleOptions $moduleOptions
248
     *
249
     * @return $this
250
     */
251
    public function setModuleOptions(ModuleOptions $moduleOptions)
252
    {
253
        $this->moduleOptions = $moduleOptions;
254
255
        return $this;
256
    }
257
258
    /**
259
     * Сервис для работы с workflow
260
     *
261
     * @return WorkflowServiceInterface
262
     */
263
    public function getWorkflowService()
264
    {
265
        return $this->workflowService;
266
    }
267
268
    /**
269
     * Устанавливает сервис для работы с workflow
270
     *
271
     * @param WorkflowServiceInterface $workflowService
272
     *
273
     * @return $this
274
     */
275
    public function setWorkflowService(WorkflowServiceInterface $workflowService)
276
    {
277
        $this->workflowService = $workflowService;
278
279
        return $this;
280
    }
281
282
283
284
    /**
285
     * @param $objectClassName
286
     * @param $objectId
287
     * @param $workflowName
288
     * @param $workflowManagerName
289
     */
290
    public function getEntryId($objectClassName, $objectId, $workflowName, $workflowManagerName)
0 ignored issues
show
Unused Code introduced by
The parameter $objectClassName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $objectId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $workflowName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $workflowManagerName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
291
    {
292
    }
293
}
294