BaseObject::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * DirectAdmin API Client
5
 * (c) Omines Internetbureau B.V. - https://omines.nl/
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Omines\DirectAdmin\Objects;
12
13
use Omines\DirectAdmin\Context\UserContext;
14
use Omines\DirectAdmin\DirectAdminException;
15
16
/**
17
 * Basic wrapper around a DirectAdmin object as observed within a specific context.
18
 *
19
 * @author Niels Keurentjes <[email protected]>
20
 */
21
abstract class BaseObject
22
{
23
    /** @var string */
24
    private $name;
25
26
    /** @var UserContext */
27
    private $context;
28
29
    /** @var array */
30
    private $cache = [];
31
32
    /**
33
     * @param string $name Canonical name for the object
34
     * @param UserContext $context Context within which the object is valid
35
     */
36
    protected function __construct($name, UserContext $context)
37
    {
38
        $this->name = $name;
39
        $this->context = $context;
40
    }
41
42
    /**
43
     * Clear the object's internal cache.
44
     */
45
    public function clearCache()
46
    {
47
        $this->cache = [];
48
    }
49
50
    /**
51
     * Retrieves an item from the internal cache.
52
     *
53
     * @param string $key Key to retrieve
54
     * @param callable|mixed $default Either a callback or an explicit default value
55
     * @return mixed Cached value
56
     */
57
    protected function getCache($key, $default)
58
    {
59
        if (!isset($this->cache[$key])) {
60
            $this->cache[$key] = is_callable($default) ? $default() : $default;
61
        }
62
        return $this->cache[$key];
63
    }
64
65
    /**
66
     * Retrieves a keyed item from inside a cache item.
67
     *
68
     * @param string $key
69
     * @param string $item
70
     * @param callable|mixed $defaultKey
71
     * @param mixed|null $defaultItem
72
     * @return mixed Cached value
73
     *
74
     * @codeCoverageIgnore
75
     */
76
    protected function getCacheItem($key, $item, $defaultKey, $defaultItem = null)
77
    {
78
        if (empty($cache = $this->getCache($key, $defaultKey))) {
79
            return $defaultItem;
80
        }
81
        if (!is_array($cache)) {
82
            throw new DirectAdminException("Cache item $key is not an array");
83
        }
84
        return isset($cache[$item]) ? $cache[$item] : $defaultItem;
85
    }
86
87
    /**
88
     * Sets a specific cache item, for when a cacheable value was a by-product.
89
     *
90
     * @param string $key
91
     * @param mixed $value
92
     */
93
    protected function setCache($key, $value)
94
    {
95
        $this->cache[$key] = $value;
96
    }
97
98
    /**
99
     * @return UserContext
100
     */
101
    public function getContext()
102
    {
103
        return $this->context;
104
    }
105
106
    /**
107
     * Protected as a derived class may want to offer the name under a different name.
108
     *
109
     * @return string
110
     */
111
    protected function getName()
112
    {
113
        return $this->name;
114
    }
115
116
    /**
117
     * Converts an array of string items to an associative array of objects of the specified type.
118
     *
119
     * @param array $items
120
     * @param string $class
121
     * @param UserContext $context
122
     * @return array
123
     */
124
    public static function toObjectArray(array $items, $class, UserContext $context)
125
    {
126
        return array_combine($items, array_map(function ($item) use ($class, $context) {
127
            return new $class($item, $context);
128
        }, $items));
129
    }
130
131
    /**
132
     * Converts an associative array of descriptors to objects of the specified type.
133
     *
134
     * @param array $items
135
     * @param string $class
136
     * @param UserContext $context
137
     * @return array
138
     */
139
    public static function toRichObjectArray(array $items, $class, UserContext $context)
140
    {
141
        array_walk($items, function (&$value, $name) use ($class, $context) {
142
            $value = new $class($name, $context, $value);
143
        });
144
        return $items;
145
    }
146
}
147