AbstractKey   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 57.89%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 78
ccs 11
cts 19
cp 0.5789
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A computeHandle() 0 4 1
A __toString() 0 4 1
A __construct() 0 6 2
A getAttributes() 0 9 2
A toHandle() 0 4 1
A toArray() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Connections\Credentials\Keys;
14
15
use Illuminate\Contracts\Support\Arrayable;
16
17
/**
18
 * Represents the credentials to something in
19
 * a way that can be easily serialized.
20
 */
21
abstract class AbstractKey implements Arrayable
22
{
23
    /**
24
     * @var string[]
25
     */
26
    protected $arrayable = [];
27
28
    /**
29
     * @param array $attributes
30
     */
31 442
    public function __construct(array $attributes = [])
32
    {
33 442
        foreach ($attributes as $key => $value) {
34 442
            $this->$key = $value;
35 442
        }
36 442
    }
37
38
    /**
39
     * Get the components to compute the handle from.
40
     *
41
     * @return string[]
42
     */
43
    protected function getAttributes()
44
    {
45
        $components = [];
46
        foreach ($this->arrayable as $attribute) {
47
            $components[] = $this->$attribute;
48
        }
49
50
        return $components;
51
    }
52
53
    //////////////////////////////////////////////////////////////////////
54
    ////////////////////////////// HANDLES ///////////////////////////////
55
    //////////////////////////////////////////////////////////////////////
56
57
    /**
58
     * Compute a simplified string version of the key.
59
     *
60
     * @return string
61
     */
62 442
    public function toHandle()
63
    {
64 442
        return $this->computeHandle($this->getAttributes());
65
    }
66
67
    /**
68
     * Compute a handle from components.
69
     *
70
     * @param string[] $components
71
     *
72
     * @return string
73
     */
74 442
    protected function computeHandle(array $components)
75
    {
76 442
        return implode('/', $components);
77
    }
78
79
    ////////////////////////////////////////////////////////////////////////////////
80
    //////////////////////////////// SERIALIZATION /////////////////////////////////
81
    ////////////////////////////////////////////////////////////////////////////////
82
83
    /**
84
     * @return string
85
     */
86 29
    public function __toString()
87
    {
88 29
        return $this->toHandle();
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function toArray()
95
    {
96
        return $this->getAttributes();
97
    }
98
}
99