AbstractResponse   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 14
dl 0
loc 133
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A str() 0 3 1
A __construct() 0 4 1
A clearCommands() 0 3 1
A plugin() 0 4 2
A getCommands() 0 3 1
A addCommand() 0 4 1
A getCommandCount() 0 3 1
A getErrorMessage() 0 3 1
A __get() 0 3 1
1
<?php
2
3
/**
4
 * AbstractResponse.php - Base class for Jaxon responses
5
 *
6
 * This class collects commands to be sent back to the browser in response to a jaxon request.
7
 * Commands are encoded and packaged in json format.
8
 *
9
 * @package jaxon-core
10
 * @author Jared White
11
 * @author J. Max Wilson
12
 * @author Joseph Woolley
13
 * @author Steffen Konerow
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
16
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
21
22
namespace Jaxon\Response;
23
24
use Jaxon\Plugin\Manager\PluginManager;
25
use Jaxon\Plugin\AbstractResponsePlugin;
26
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
27
use JsonSerializable;
28
29
abstract class AbstractResponse
30
{
31
    /**
32
     * @var ResponseManager
33
     */
34
    protected $xManager;
35
36
    /**
37
     * @var PluginManager
38
     */
39
    protected $xPluginManager;
40
41
    /**
42
     * The constructor
43
     *
44
     * @param ResponseManager $xManager
45
     * @param PluginManager $xPluginManager
46
     */
47
    public function __construct(ResponseManager $xManager, PluginManager $xPluginManager)
48
    {
49
        $this->xManager = $xManager;
50
        $this->xPluginManager = $xPluginManager;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    abstract public function getContentType(): string;
57
58
    /**
59
     * @inheritDoc
60
     */
61
    abstract public function getOutput(): string;
62
63
    /**
64
     * Convert this response to a PSR7 response object
65
     *
66
     * @return PsrResponseInterface
67
     */
68
    abstract public function toPsr(): PsrResponseInterface;
69
70
    /**
71
     * Convert to string
72
     *
73
     * @param mixed $xData
74
     *
75
     * @return string
76
     */
77
    protected function str($xData): string
78
    {
79
        return $this->xManager->str($xData);
80
    }
81
82
    /**
83
     * Get the error message
84
     *
85
     * @return string
86
     */
87
    public function getErrorMessage(): string
88
    {
89
        return $this->xManager->getErrorMessage();
90
    }
91
92
     /**
93
     * Add a response command to the array of commands
94
     *
95
     * @param string $sName    The command name
96
     * @param array|JsonSerializable $aArgs    The command arguments
97
     * @param bool $bRemoveEmpty
98
     *
99
     * @return Command
100
     */
101
    public function addCommand(string $sName, array|JsonSerializable $aArgs = [],
102
        bool $bRemoveEmpty = false): Command
103
    {
104
        return $this->xManager->addCommand($sName, $aArgs, $bRemoveEmpty);
105
    }
106
107
    /**
108
     * Get the commands in the response
109
     *
110
     * @return array
111
     */
112
    public function getCommands(): array
113
    {
114
        return $this->xManager->getCommands();
115
    }
116
117
    /**
118
     * Get the number of commands in the response
119
     *
120
     * @return int
121
     */
122
    public function getCommandCount(): int
123
    {
124
        return $this->xManager->getCommandCount();
125
    }
126
127
    /**
128
     * Clear all the commands already added to the response
129
     *
130
     * @return void
131
     */
132
    public function clearCommands()
133
    {
134
        $this->xManager->clearCommands();
135
    }
136
137
    /**
138
     * Provides access to registered response plugins
139
     *
140
     * @param string $sName    The name of the plugin
141
     *
142
     * @return null|AbstractResponsePlugin
143
     */
144
    public function plugin(string $sName): ?AbstractResponsePlugin
145
    {
146
        $xResponsePlugin = $this->xPluginManager->getResponsePlugin($sName);
147
        return !$xResponsePlugin ? null : $xResponsePlugin->_init($this);
148
    }
149
150
    /**
151
     * Magic PHP function
152
     *
153
     * Used to permit plugins to be called as if they were native members of the Response instance.
154
     *
155
     * @param string $sPluginName    The name of the plugin
156
     *
157
     * @return null|AbstractResponsePlugin
158
     */
159
    public function __get(string $sPluginName)
160
    {
161
        return $this->plugin($sPluginName);
162
    }
163
}
164