ConfirmationFactory   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 15
c 4
b 0
f 2
lcom 2
cbo 1
dl 0
loc 132
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 1
A __construct() 0 5 1
A getSettingsParameter() 0 14 2
A setActivedService() 0 8 3
A add() 0 8 2
A create() 0 15 3
A createActivedConfirmation() 0 12 3
1
<?php
2
3
namespace DoS\UserBundle\Confirmation;
4
5
use Sylius\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
6
7
class ConfirmationFactory
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $types = array();
13
14
    /**
15
     * @var array
16
     */
17
    protected $options = array();
18
19
    /**
20
     * @var null|string
21
     */
22
    protected $activedService = null;
23
24
    /**
25
     * @var SettingsManagerInterface
26
     */
27
    protected $settingsManager;
28
29
    public function __construct(SettingsManagerInterface $settingsManager)
30
    {
31
        $this->settingsManager = $settingsManager;
32
        // TODO: apply options from settings
33
    }
34
35
    /**
36
     * Load settings parameter for given namespace and name.
37
     *
38
     * @param string $name
39
     *
40
     * @return mixed
41
     *
42
     * @throws \InvalidArgumentException
43
     */
44
    protected function getSettingsParameter($name)
45
    {
46
        if (false === strpos($name, '.')) {
47
            throw new \InvalidArgumentException(
48
                sprintf('Parameter must be in format "namespace.name", "%s" given.', $name)
49
            );
50
        }
51
52
        list($namespace, $name) = explode('.', $name);
53
54
        $settings = $this->settingsManager->load($namespace);
55
56
        return $settings->get($name);
57
    }
58
59
    /**
60
     * @param $name
61
     *
62
     * @throws \Exception
63
     */
64
    public function setActivedService($name)
65
    {
66
        if ($name && !$this->has($name)) {
67
            throw new \Exception(sprintf('Cannot set unregistered confirmation type "%s".', $name));
68
        }
69
70
        $this->activedService = $name;
71
    }
72
73
    /**
74
     * @param ConfirmationInterface $confirmation
75
     * @param array                 $options
76
     */
77
    public function add(ConfirmationInterface $confirmation, array $options = array())
78
    {
79
        $this->types[$confirmation->getType()] = $confirmation;
80
81
        if (!empty($options)) {
82
            $this->options[$confirmation->getType()] = $options;
83
        }
84
    }
85
86
    /**
87
     * @param $name
88
     *
89
     * @return bool
90
     */
91
    public function has($name)
92
    {
93
        return array_key_exists($name, $this->types);
94
    }
95
96
    /**
97
     * @param $name
98
     *
99
     * @return ConfirmationInterface
100
     *
101
     * @throws \Exception
102
     */
103
    public function create($name)
104
    {
105
        if (!$this->has($name)) {
106
            throw new \Exception(sprintf('Not found confirmation type "%s".', $name));
107
        }
108
109
        /** @var ConfirmationInterface $instance */
110
        $instance = $this->types[$name];
111
112
        if (array_key_exists($name, $this->options)) {
113
            $instance->resetOptions($this->options[$name]);
114
        }
115
116
        return $instance;
117
    }
118
119
    /**
120
     * @param bool $invalidException
121
     *
122
     * @return ConfirmationInterface|null
123
     *
124
     * @throws \Exception
125
     */
126
    public function createActivedConfirmation($invalidException = false)
127
    {
128
        try {
129
            return $this->create($this->activedService);
130
        } catch (\Exception $e) {
131
            if ($invalidException) {
132
                throw $e;
133
            }
134
        }
135
136
        return;
137
    }
138
}
139