1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Global Trading Technologies Ltd workflow-extension-bundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* (c) fduch <[email protected]> |
9
|
|
|
* @date 29.07.16 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gtt\Bundle\WorkflowExtensionsBundle\WorkflowSubject; |
13
|
|
|
|
14
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Exception\SubjectIdRetrievingException; |
15
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Exception\SubjectManipulatorException; |
16
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Exception\SubjectRetrievingFromDomainException; |
17
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class helps to retrieve workflow subject from domain and retrieve id from subject used by scheduler subsystem |
21
|
|
|
*/ |
22
|
|
|
class SubjectManipulator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Expression language |
26
|
|
|
* |
27
|
|
|
* @var ExpressionLanguage |
28
|
|
|
*/ |
29
|
|
|
private $language; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Holds expressions used to retrieve subject from domain and id from subject |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $supportedSubjectsConfig = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* SubjectManipulator constructor. |
40
|
|
|
* |
41
|
|
|
* @param ExpressionLanguage $language expression language |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ExpressionLanguage $language) |
44
|
|
|
{ |
45
|
|
|
$this->language = $language; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets expressions for supported subject |
50
|
|
|
* |
51
|
|
|
* @param string $subjectClass subject class |
52
|
|
|
* @param string $idFromSubjectExpression expression used to retrieve subject id from subject object |
53
|
|
|
* @param string|null $subjectFromDomainExpression expression used to retrieve subject from domain |
54
|
|
|
* |
55
|
|
|
* @throws SubjectManipulatorException |
56
|
|
|
*/ |
57
|
|
|
public function addSupportedSubject($subjectClass, $idFromSubjectExpression, $subjectFromDomainExpression = null) |
58
|
|
|
{ |
59
|
|
|
if (isset($this->supportedSubjectsConfig[$subjectClass])) { |
60
|
|
|
throw SubjectManipulatorException::subjectConfigIsAlreadySet($subjectClass); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->supportedSubjectsConfig[$subjectClass] = [ |
64
|
|
|
'id_from_subject' => $idFromSubjectExpression, |
65
|
|
|
'subject_from_domain' => $subjectFromDomainExpression |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Retrieves workflow subject from domain |
71
|
|
|
* |
72
|
|
|
* @param string $subjectClass subject class |
73
|
|
|
* @param int $subjectId subject id |
74
|
|
|
* |
75
|
|
|
* @return object |
76
|
|
|
* |
77
|
|
|
* @throws SubjectRetrievingFromDomainException |
78
|
|
|
*/ |
79
|
|
|
public function getSubjectFromDomain($subjectClass, $subjectId) |
80
|
|
|
{ |
81
|
|
|
$subjectClass = ltrim($subjectClass, "\\"); |
82
|
|
|
if (!array_key_exists($subjectClass, $this->supportedSubjectsConfig) || |
83
|
|
|
!array_key_exists('subject_from_domain', $this->supportedSubjectsConfig[$subjectClass])) { |
84
|
|
|
throw SubjectRetrievingFromDomainException::expressionNotFound($subjectClass); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->language->evaluate( |
88
|
|
|
$this->supportedSubjectsConfig[$subjectClass]['subject_from_domain'], |
89
|
|
|
['subjectClass' => $subjectClass, 'subjectId' => $subjectId] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Fetches subject id from subject object |
95
|
|
|
* |
96
|
|
|
* @param object $subject subject |
97
|
|
|
* |
98
|
|
|
* @return int |
99
|
|
|
* |
100
|
|
|
* @throws SubjectIdRetrievingException |
101
|
|
|
*/ |
102
|
|
|
public function getSubjectId($subject) |
103
|
|
|
{ |
104
|
|
|
if (!is_object($subject)) { |
105
|
|
|
throw SubjectIdRetrievingException::subjectIsNotAnObject($subject); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$subjectClass = get_class($subject); |
109
|
|
|
if (!array_key_exists($subjectClass, $this->supportedSubjectsConfig) || |
110
|
|
|
!array_key_exists('id_from_subject', $this->supportedSubjectsConfig[$subjectClass])) { |
111
|
|
|
throw SubjectIdRetrievingException::expressionNotFound($subjectClass); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->language->evaluate( |
115
|
|
|
$this->supportedSubjectsConfig[$subjectClass]['id_from_subject'], |
116
|
|
|
['subject' => $subject] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |