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 ( 4095d1...89bae3 )
by Андрей
04:02
created

DoctrineWorkflowStoryService::getSerializer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
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
     * Сериалайзер
61
     *
62
     * @var Serializer
63
     */
64
    protected $serializer;
65
66
    /**
67
     * DoctrineWorkflowStoryService constructor.
68
     *
69
     * @param array $options
70
     */
71
    public function __construct(array $options = [])
72
    {
73
        $initOptions = [
74
            array_key_exists('serializerManager', $options) ? $options['serializerManager'] : null,
75
            array_key_exists('moduleOptions', $options) ? $options['moduleOptions'] : null,
76
            array_key_exists('workflowService', $options) ? $options['workflowService'] : null
77
        ];
78
        call_user_func_array([$this, 'init'], $initOptions);
79
    }
80
81
    /**
82
     * @param SerializerManager        $serializerManager
83
     * @param ModuleOptions            $moduleOptions
84
     * @param WorkflowServiceInterface $workflowService
85
     */
86
    protected function init(SerializerManager $serializerManager, ModuleOptions $moduleOptions, WorkflowServiceInterface $workflowService)
87
    {
88
        $this->setSerializerManager($serializerManager);
89
        $this->setModuleOptions($moduleOptions);
90
        $this->setWorkflowService($workflowService);
91
    }
92
93
    /**
94
     * @WFS\ArgumentsMap(argumentsMap={
95
     *      @WFS\Map(fromArgName="entryParam", to="entry"),
96
     *      @WFS\Map(fromArgName="objectParam", to="object"),
97
     *      @WFS\Map(fromArgName="objectAliasParam", to="objectAlias"),
98
     *      @WFS\Map(fromArgName="storeParam", to="store")
99
     *})
100
     *
101
     *
102
     * @param ExtEntry               $entry
103
     * @param mixed                  $object
104
     * @param string                 $objectAlias
105
     * @param DoctrineWorkflowStory $store
106
     *
107
     * @throws Exception\InvalidWorkflowStoreException
108
     * @throws \OldTown\Workflow\Spi\Doctrine\Exception\DoctrineRuntimeException
109
     * @throws Exception\RuntimeException
110
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
111
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
112
     * @throws \Zend\ServiceManager\Exception\RuntimeException
113
     * @throws \Zend\Serializer\Exception\ExceptionInterface
114
     */
115
    public function bindObjectToWorkflowEntry(ExtEntry $entry, $object, $objectAlias = self::DEFAULT_OBJECT, DoctrineWorkflowStory $store)
116
    {
117
        $em = $store->getEntityManager();
118
119
        $objectClass = get_class($object);
120
        $metadata = $em->getClassMetadata($objectClass);
121
122
        $serializerName = $this->getSerializerName();
123
        /** @var Serializer $serializer */
124
        $serializer = $this->getSerializerManager()->get($serializerName);
125
126
        $id = $metadata->getIdentifierValues($object);
127
        $prepareId = [];
128
        foreach ($id as $idField => $idValue) {
129
            $prepareId[$idField] = (string)$idValue;
130
        }
131
        $serializedId = $serializer->serialize($prepareId);
132
133
        $objectInfoClass = $this->getModuleOptions()->getEntityClassName('DoctrineWorkflowStory\\ObjectInfo');
134
135
        $r = new ReflectionClass($objectInfoClass);
136
        /** @var  ObjectInfo $objectInfo */
137
        $objectInfo = $r->newInstance();
138
139
        $objectInfo->setClassName($objectClass);
140
        $objectInfo->setObjectId($serializedId);
141
        $objectInfo->setAlias($objectAlias);
142
        $objectInfo->addEntry($entry);
143
144
145
        $entry->addObjectInfo($objectInfo);
146
147
        $em->persist($objectInfo);
148
        $em->flush();
149
    }
150
151
    /**
152
     * Востановить объект привязанный к процессу
153
     *
154
     * @WFS\ArgumentsMap(argumentsMap={
155
     *      @WFS\Map(fromArgName="entryParam", to="entry"),
156
     *      @WFS\Map(fromArgName="objectAliasParam", to="objectAlias"),
157
     *      @WFS\Map(fromArgName="storeParam", to="store")
158
     * })
159
     *
160
     * @WFS\ResultVariable(name="resultVariableName")
161
     *
162
     *
163
     * @param ExtEntry              $entry
164
     * @param string                $objectAlias
165
     * @param DoctrineWorkflowStory $store
166
     *
167
     * @return mixed
168
     *
169
     * @throws \OldTown\Workflow\Spi\Doctrine\Exception\DoctrineRuntimeException
170
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
171
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
172
     * @throws \Zend\ServiceManager\Exception\RuntimeException
173
     * @throws \Zend\Serializer\Exception\ExceptionInterface
174
     * @throws Exception\InvalidRestoreObjectException
175
     * @throws Exception\InvalidArgumentException
176
     */
177
    public function restoreObjectBindingToEntry(ExtEntry $entry, $objectAlias = self::DEFAULT_OBJECT, DoctrineWorkflowStory $store)
178
    {
179
        $objectsInfo = $entry->getObjectsInfo();
180
181
        foreach ($objectsInfo as $objectInfo) {
182
            if ($objectAlias === $objectInfo->getAlias()) {
183
                $className = $objectInfo->getClassName();
184
185
                $em = $store->getEntityManager();
186
187
                $serializer = $this->getSerializer();
188
189
                $serializedId = $objectInfo->getObjectId();
190
                $id = $serializer->unserialize($serializedId);
191
192
                $object = $em->getRepository($className)->find($id);
193
194
                if (!is_object($object)) {
195
                    $errMsg = sprintf('Invalid restore object. Alias: %s. Class: %s. Id: %s', $objectAlias, $className, $serializedId);
196
                    throw new Exception\InvalidRestoreObjectException($errMsg);
197
                }
198
199
                return $object;
200
            }
201
        }
202
203
        $errMsg = sprintf('Invalid object alias: %s', $objectAlias);
204
        throw new Exception\InvalidArgumentException($errMsg);
205
    }
206
207
    /**
208
     * @return SerializerManager
209
     */
210
    public function getSerializerManager()
211
    {
212
        return $this->serializerManager;
213
    }
214
215
    /**
216
     * @param SerializerManager $serializerManager
217
     *
218
     * @return $this
219
     */
220
    public function setSerializerManager(SerializerManager $serializerManager)
221
    {
222
        $this->serializerManager = $serializerManager;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @return string
229
     */
230
    public function getSerializerName()
231
    {
232
        return $this->serializerName;
233
    }
234
235
    /**
236
     * @param string $serializerName
237
     *
238
     * @return $this
239
     */
240
    public function setSerializerName($serializerName)
241
    {
242
        $this->serializerName = $serializerName;
243
244
        return $this;
245
    }
246
247
    /**
248
     * @return ModuleOptions
249
     */
250
    public function getModuleOptions()
251
    {
252
        return $this->moduleOptions;
253
    }
254
255
    /**
256
     * @param ModuleOptions $moduleOptions
257
     *
258
     * @return $this
259
     */
260
    public function setModuleOptions(ModuleOptions $moduleOptions)
261
    {
262
        $this->moduleOptions = $moduleOptions;
263
264
        return $this;
265
    }
266
267
    /**
268
     * Сервис для работы с workflow
269
     *
270
     * @return WorkflowServiceInterface
271
     */
272
    public function getWorkflowService()
273
    {
274
        return $this->workflowService;
275
    }
276
277
    /**
278
     * Устанавливает сервис для работы с workflow
279
     *
280
     * @param WorkflowServiceInterface $workflowService
281
     *
282
     * @return $this
283
     */
284
    public function setWorkflowService(WorkflowServiceInterface $workflowService)
285
    {
286
        $this->workflowService = $workflowService;
287
288
        return $this;
289
    }
290
291
292
293
    /**
294
     * @param $objectClassName
295
     * @param $objectId
296
     * @param $workflowName
297
     * @param $workflowManagerName
298
     */
299
    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...
300
    {
301
    }
302
303
    /**
304
     * @return Serializer
305
     *
306
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
307
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
308
     * @throws \Zend\ServiceManager\Exception\RuntimeException
309
     */
310
    public function getSerializer()
311
    {
312
        if ($this->serializer) {
313
            return $this->serializer;
314
        }
315
316
        $serializerName = $this->getSerializerName();
317
        /** @var Serializer $serializer */
318
        $serializer = $this->getSerializerManager()->get($serializerName);
319
        $this->serializer = $serializer;
320
321
        return $this->serializer;
322
    }
323
}
324