VariableResolver::resolve()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 3
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Slides\Connector\Auth\Clients\Mandrill;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * Class VariableResolver
11
 *
12
 * @package Slides\Connector\Auth\Clients\Mandrill
13
 */
14
class VariableResolver
15
{
16
    /**
17
     * @var Collection
18
     */
19
    protected $emails;
20
21
    /**
22
     * @var array
23
     */
24
    protected $context;
25
26
    /**
27
     * VariableResolver constructor.
28
     *
29
     * @param Collection $emails
30
     * @param array $context
31
     */
32
    final public function __construct(Collection $emails, array $context)
33
    {
34
        $this->emails = $emails;
35
        $this->context = $context;
36
37
        if (method_exists($this, 'boot')) {
38
            app()->call([$this, 'boot']);
39
        }
40
    }
41
42
    /**
43
     * Resolver given variable.
44
     *
45
     * @param string $name
46
     * @param string $email
47
     *
48
     * @return array
49
     *
50
     * @throws Exception
51
     */
52
    final public function resolve(string $name, string $email): array
53
    {
54
        $method = 'get' . ucfirst($name) . 'Variable';
55
56
        if (!method_exists($this, $method)) {
57
            throw new Exception('Method ' . $method . ' should be defined.');
58
        }
59
60
        return [
61
            'name' => $name,
62
            'content' => call_user_func([$this, $method], $email)
63
        ];
64
    }
65
66
    /**
67
     * Get the variables from the context.
68
     *
69
     * @param string $key
70
     * @param mixed $default
71
     *
72
     * @return mixed
73
     */
74
    protected function get(string $key, $default = null)
75
    {
76
        return Arr::get($this->context, $key, $default);
77
    }
78
}