-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathConditionFactory.php
60 lines (54 loc) · 1.67 KB
/
ConditionFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Rule\Model;
use Magento\Framework\ObjectManagerInterface;
use Magento\Rule\Model\Condition\ConditionInterface;
class ConditionFactory
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* Store all used condition models
*
* @var array
*/
private $conditionModels = [];
/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* Create new object for each requested model.
* If model is requested first time, store it at array.
* It's made by performance reasons to avoid initialization of same models each time when rules are being processed.
*
* @param string $type
*
* @return \Magento\Rule\Model\Condition\ConditionInterface
*
* @throws \LogicException
* @throws \BadMethodCallException
* @throws \InvalidArgumentException
*/
public function create($type)
{
if (!array_key_exists($type, $this->conditionModels)) {
if (!class_exists($type)) {
throw new \InvalidArgumentException('Class does not exist');
}
if (!in_array(ConditionInterface::class, class_implements($type))) {
throw new \InvalidArgumentException('Class does not implement condition interface');
}
$this->conditionModels[$type] = $this->objectManager->create($type);
}
return clone $this->conditionModels[$type];
}
}