AbstractAdapter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 92
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A isSandbox() 0 4 1
A setSandbox() 0 6 1
A getDefinedParameters() 0 4 1
A getDefaultParameters() 0 4 1
A getRequiredParameters() 0 4 1
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify\Adapter;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Jgut\Tify\Exception\AdapterException;
14
use Jgut\Tify\ParameterTrait;
15
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
/**
19
 * Class AbstractService.
20
 */
21
abstract class AbstractAdapter
22
{
23
    use ParameterTrait;
24
25
    /**
26
     * Sandbox environment.
27
     *
28
     * @var bool
29
     */
30
    protected $sandbox;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param array $parameters
36
     * @param bool  $sandbox
37
     *
38
     * @throws \Jgut\Tify\Exception\AdapterException
39
     */
40
    public function __construct(array $parameters = [], $sandbox = false)
41
    {
42
        $parametersResolver = new OptionsResolver();
43
44
        $parametersResolver->setDefined($this->getDefinedParameters());
45
        $parametersResolver->setDefaults($this->getDefaultParameters());
46
        $parametersResolver->setRequired($this->getRequiredParameters());
47
48
        try {
49
            $this->parameters = new ArrayCollection($parametersResolver->resolve($parameters));
50
        } catch (MissingOptionsException $exception) {
51
            throw new AdapterException(sprintf('Missing parameters on "%s"', self::class));
52
        } catch (\Exception $exception) {
53
            throw new AdapterException('Invalid parameter provided');
54
        }
55
56
        $this->sandbox = (bool) $sandbox;
57
    }
58
59
    /**
60
     * Retrieve if sandbox.
61
     *
62
     * @return bool
63
     */
64
    public function isSandbox()
65
    {
66
        return (bool) $this->sandbox;
67
    }
68
69
    /**
70
     * Set Sandbox.
71
     *
72
     * @param bool $sandbox
73
     *
74
     * @return $this
75
     */
76
    public function setSandbox($sandbox)
77
    {
78
        $this->sandbox = (bool) $sandbox;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Get the list of defined parameters.
85
     *
86
     * @return array
87
     */
88
    protected function getDefinedParameters()
89
    {
90
        return [];
91
    }
92
93
    /**
94
     * Get the list of default parameters.
95
     *
96
     * @return array
97
     */
98
    protected function getDefaultParameters()
99
    {
100
        return [];
101
    }
102
103
    /**
104
     * Get the list of required parameters.
105
     *
106
     * @return array
107
     */
108
    protected function getRequiredParameters()
109
    {
110
        return [];
111
    }
112
}
113