Completed
Push — master ( c622f7...33a06a )
by Richard
09:52 queued 02:32
created

Provider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.64%

Importance

Changes 0
Metric Value
dl 0
loc 145
ccs 39
cts 44
cp 0.8864
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProviderMode() 0 13 3
A register() 0 11 3
A getRegistered() 0 4 1
A sortProviders() 0 12 3
A isAvailable() 0 4 1
A __callStatic() 0 4 1
A __call() 0 22 3
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Service;
13
14
/**
15
 * Service Provider object
16
 *
17
 * All provider classes should extend this class, and implement the appropriate
18
 * contract interface.
19
 *
20
 * @category  Xoops\Core\Service\Provider
21
 * @package   Xoops\Core
22
 * @author    Richard Griffith <[email protected]>
23
 * @copyright 2013-2015 The XOOPS Project https://github.com/XOOPS/XoopsCore
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @version   Release: 1.0
26
 * @link      http://xoops.org
27
 *
28
 * @method Response getAvatarUrl(mixed $userinfo);
29
 * @method Response getAvatarEditUrl(\XoopsUser $userinfo);
30
 * @method Response getImgUrl(string $value, mixed $p2=null, mixed $d3=null, mixed $d4=null, mixed $d5=null);
31
 * @method Response getImgTag(string $value, mixed $d2=null, mixed $d3=null, mixed $d4=null, mixed $d5=null);
32
 * @method Response startPdf();
33
 * @method Response setPageOrientation(string $pageOrientation);
34
 * @method Response setPageSize(string $pageSize);
35
 * @method Response setBaseUnit(string $unit);
36
 * @method Response setMargins(float $leftMargin, float $topMargin, float $rightMargin, float $bottomMargin);
37
 * @method Response setBaseFont(string $fontFamily, string $fontStyle, float|null $fontSize);
38
 * @method Response setDefaultMonospacedFont(string $monoFontFamily);
39
 * @method Response setAuthor(string $pdfAuthor);
40
 * @method Response setTitle(string $pdfTitle);
41
 * @method Response setSubject(string $pdfSubject);
42
 * @method Response setKeywords(array $pdfKeywords);
43
 * @method Response addHtml(string $html);
44
 * @method Response outputPdfInline(string $name);
45
 * @method Response outputPdfDownload(string $name);
46
 * @method Response fetchPdf();
47
 * @method Response getUserRank(mixed $userinfo);
48
 * @method Response getAssignableUserRankList();
49
 * @method Response renderEmoji(string $buffer);
50
 * @method Response getEmojiList();
51
 * @method Response renderEmojiSelector(string $identifier);
52
 */
53
class Provider
54
{
55
    protected $manager = null;
56
57
    protected $service = null;
58
59
    /** @var AbstractContract[] $providers */
60
    protected $providers = array();
61
62
    /**
63
     * __construct
64
     *
65
     * @param Manager $manager Manager instance
66
     * @param string  $service service name (case sensitive)
67
     */
68 8
    public function __construct(Manager $manager, $service)
69
    {
70 8
        $this->manager = $manager;
71 8
        $this->service = $service;
72 8
    }
73
74
    /**
75
     * getProviderMode
76
     *
77
     * @return Manager MODE constant
78
     */
79 2
    public function getProviderMode()
80
    {
81 2
        static $ret = null;
82
83 2
        if ($ret===null) {
84 1
            if (count($this->providers)) {
85 1
                $ret = reset($this->providers)->getMode();
86
            } else {
87
                return Manager::MODE_EXCLUSIVE;
88
            }
89
        }
90 2
        return $ret;
91
    }
92
93
    /**
94
     * registerProvider - register a provider of a named service
95
     *
96
     * @param string $object instantiated object that provides the service
97
     *
98
     * @return void
99
     */
100 1
    public function register($object)
101
    {
102
        // verify this is the proper type of object
103 1
        $contract = '\Xoops\Core\Service\Contract\\' . $this->service . 'Interface';
104
105 1
        if (is_a($object, '\Xoops\Core\Service\AbstractContract')
106 1
            && $object instanceof $contract
107
        ) {
108 1
            $this->providers[] = $object;
109
        }
110 1
    }
111
112
    /**
113
     * getRegistered - access list of registered providers
114
     *
115
     * @return array of registered providers managed by this instance
116
     */
117 2
    public function &getRegistered()
118
    {
119 2
        return $this->providers;
120
    }
121
122
    /**
123
     * sortProviders - sort providers into priority order
124
     *
125
     * @return void
126
     */
127 2
    public function sortProviders()
128
    {
129 2
        $sortable = $this->providers;
130 2
        usort($sortable, function (AbstractContract $a, AbstractContract $b) {
131 1
            if ($a->getPriority() != $b->getPriority()) {
132 1
                return ($a->getPriority() > $b->getPriority()) ? 1 : -1;
133
            } else {
134
                return 0;
135
            }
136 2
        });
137 2
        $this->providers = $sortable;
138 2
    }
139
140
    /**
141
     * isAvailable - indicate the availability of an actual provider
142
     *
143
     * In many cases a null provider can be called without changing the flow of the calling
144
     * program. In some cases, the availability of a provider may need to be reflected in
145
     * the caller, i.e. adding a UI button or menu item.
146
     *
147
     * @return boolean true if actual provider is available, otherwise false
148
     */
149 1
    public function isAvailable()
150
    {
151 1
        return true;
152
    }
153
154
    /**
155
     * All contract specified methods go here
156
     *
157
     * @param string $name      method to call
158
     * @param mixed  $arguments any arguments
159
     *
160
     * @return Response Response
161
     */
162 2
    public function __call($name, $arguments)
163
    {
164 2
        $mode = $this->getProviderMode();
0 ignored issues
show
Unused Code introduced by
$mode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
165
166
        // for right now only one provider will be called, and it should be at the top
167 2
        $object = reset($this->providers);
168 2
        $method = array($object, $name);
169 2
        $response = new Response();
170 2
        if (is_callable($method)) {
171
            try {
172
                //$object->$name($response, $arguments);
173 2
                array_unshift($arguments, $response);
174 2
                call_user_func_array($method, $arguments);
175
            } catch (\Exception $e) {
176
                \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
177
                $response->setSuccess(false)->addErrorMessage($e->getMessage());
178
            }
179
        } else {
180
            $response->setSuccess(false)->addErrorMessage(sprintf('No method %s', $name));
181
        }
182 2
        return $response;
183
    }
184
185
    /**
186
     * All static methods go here and will return null
187
     *
188
     * @param string $name      not used
189
     * @param mixed  $arguments not used
190
     *
191
     * @return null
192
     */
193 1
    public static function __callStatic($name, $arguments)
194
    {
195 1
        return null;
196
    }
197
}
198