1 | <?php |
||
2 | /* |
||
3 | * @copyright 2018 Mautic Contributors. All rights reserved |
||
4 | * @author Mautic |
||
5 | * |
||
6 | * @link http://mautic.org |
||
7 | * |
||
8 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
9 | */ |
||
10 | |||
11 | namespace Mautic\LeadBundle\Segment; |
||
12 | |||
13 | use Mautic\LeadBundle\Segment\Decorator\FilterDecoratorInterface; |
||
14 | use Mautic\LeadBundle\Segment\DoNotContact\DoNotContactParts; |
||
15 | use Mautic\LeadBundle\Segment\Exception\FieldNotFoundException; |
||
16 | use Mautic\LeadBundle\Segment\IntegrationCampaign\IntegrationCampaignParts; |
||
17 | use Mautic\LeadBundle\Segment\Query\Filter\FilterQueryBuilderInterface; |
||
18 | use Mautic\LeadBundle\Segment\Query\QueryBuilder; |
||
19 | |||
20 | /** |
||
21 | * Class ContactSegmentFilter is used for accessing $filter as an object and to keep logic in an object. |
||
22 | */ |
||
23 | class ContactSegmentFilter |
||
24 | { |
||
25 | /** |
||
26 | * @var ContactSegmentFilterCrate |
||
27 | */ |
||
28 | public $contactSegmentFilterCrate; |
||
29 | |||
30 | /** |
||
31 | * @var FilterDecoratorInterface |
||
32 | */ |
||
33 | private $filterDecorator; |
||
34 | |||
35 | /** |
||
36 | * @var FilterQueryBuilderInterface |
||
37 | */ |
||
38 | private $filterQueryBuilder; |
||
39 | |||
40 | /** |
||
41 | * @var TableSchemaColumnsCache |
||
42 | */ |
||
43 | private $schemaCache; |
||
44 | |||
45 | public function __construct( |
||
46 | ContactSegmentFilterCrate $contactSegmentFilterCrate, |
||
47 | FilterDecoratorInterface $filterDecorator, |
||
48 | TableSchemaColumnsCache $cache, |
||
49 | FilterQueryBuilderInterface $filterQueryBuilder |
||
50 | ) { |
||
51 | $this->contactSegmentFilterCrate = $contactSegmentFilterCrate; |
||
52 | $this->filterDecorator = $filterDecorator; |
||
53 | $this->schemaCache = $cache; |
||
54 | $this->filterQueryBuilder = $filterQueryBuilder; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return \Doctrine\DBAL\Schema\Column |
||
59 | * |
||
60 | * @throws FieldNotFoundException |
||
61 | */ |
||
62 | public function getColumn() |
||
63 | { |
||
64 | $currentDBName = $this->schemaCache->getCurrentDatabaseName(); |
||
65 | |||
66 | $table = preg_replace("/^{$currentDBName}\./", '', $this->getTable()); |
||
67 | |||
68 | $columns = $this->schemaCache->getColumns($table); |
||
69 | |||
70 | if (!isset($columns[$this->getField()])) { |
||
71 | throw new FieldNotFoundException(sprintf('Database schema does not contain field %s.%s', $this->getTable(), $this->getField())); |
||
72 | } |
||
73 | |||
74 | return $columns[$this->getField()]; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getQueryType() |
||
81 | { |
||
82 | return $this->filterDecorator->getQueryType($this->contactSegmentFilterCrate); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return string|null |
||
87 | */ |
||
88 | public function getOperator() |
||
89 | { |
||
90 | return $this->filterDecorator->getOperator($this->contactSegmentFilterCrate); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return mixed |
||
95 | */ |
||
96 | public function getField() |
||
97 | { |
||
98 | return $this->filterDecorator->getField($this->contactSegmentFilterCrate); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function getTable() |
||
105 | { |
||
106 | return $this->filterDecorator->getTable($this->contactSegmentFilterCrate); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $argument |
||
111 | * |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function getParameterHolder($argument) |
||
115 | { |
||
116 | return $this->filterDecorator->getParameterHolder($this->contactSegmentFilterCrate, $argument); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function getParameterValue() |
||
123 | { |
||
124 | return $this->filterDecorator->getParameterValue($this->contactSegmentFilterCrate); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return string|null |
||
129 | */ |
||
130 | public function getWhere() |
||
131 | { |
||
132 | return $this->filterDecorator->getWhere($this->contactSegmentFilterCrate); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return string|null |
||
137 | */ |
||
138 | public function getGlue() |
||
139 | { |
||
140 | return $this->contactSegmentFilterCrate->getGlue(); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function getAggregateFunction() |
||
147 | { |
||
148 | return $this->filterDecorator->getAggregateFunc($this->contactSegmentFilterCrate); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return FilterQueryBuilderInterface |
||
153 | */ |
||
154 | public function getFilterQueryBuilder() |
||
155 | { |
||
156 | return $this->filterQueryBuilder; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return QueryBuilder |
||
161 | */ |
||
162 | public function applyQuery(QueryBuilder $queryBuilder) |
||
163 | { |
||
164 | return $this->filterQueryBuilder->applyQuery($queryBuilder, $this); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Whether the filter references another ContactSegment. |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function isContactSegmentReference() |
||
173 | { |
||
174 | return 'leadlist' === $this->getField(); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function isColumnTypeBoolean() |
||
181 | { |
||
182 | return $this->contactSegmentFilterCrate->isBooleanType(); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return mixed |
||
187 | */ |
||
188 | public function getNullValue() |
||
189 | { |
||
190 | return $this->contactSegmentFilterCrate->getNullValue(); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return DoNotContactParts |
||
195 | */ |
||
196 | public function getDoNotContactParts() |
||
197 | { |
||
198 | return new DoNotContactParts($this->contactSegmentFilterCrate->getField()); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return IntegrationCampaignParts |
||
203 | */ |
||
204 | public function getIntegrationCampaignParts() |
||
205 | { |
||
206 | return new IntegrationCampaignParts($this->getParameterValue()); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
207 | } |
||
208 | |||
209 | public function __toString() |
||
210 | { |
||
211 | return sprintf( |
||
212 | 'table: %s, %s on %s %s %s', |
||
213 | $this->getTable(), |
||
214 | $this->getField(), |
||
215 | $this->getQueryType(), |
||
216 | $this->getOperator(), |
||
217 | json_encode($this->getParameterValue()) |
||
218 | ); |
||
219 | } |
||
220 | |||
221 | public function getRelationJoinTable() |
||
222 | { |
||
223 | return method_exists($this->filterDecorator, 'getRelationJoinTable') ? $this->filterDecorator->getRelationJoinTable() : null; |
||
224 | } |
||
225 | |||
226 | public function getRelationJoinTableField() |
||
227 | { |
||
228 | return method_exists($this->filterDecorator, 'getRelationJoinTableField') ? |
||
229 | $this->filterDecorator->getRelationJoinTableField() : null; |
||
230 | } |
||
231 | } |
||
232 |