AbstractResponderLocator::negotiate()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
/**
3
 * Vperyod\AcceptHandler
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Responder
13
 * @package   Vperyod\AcceptHandler
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      https://github.com/vperyod/vperyod.accept-handler
18
 */
19
20
namespace Vperyod\AcceptHandler\Responder;
21
22
use Vperyod\AcceptHandler\Exception;
23
24
use Psr\Http\Message\ResponseInterface as Response;
25
use Psr\Http\Message\ServerRequestInterface as Request;
26
27
/**
28
 * ResponderLocator
29
 *
30
 * @category Responder
31
 * @package  Vperyod\AcceptHandler
32
 * @author   Jake Johns <[email protected]>
33
 * @license  http://jnj.mit-license.org/ MIT License
34
 * @link     https://github.com/vperyod/vperyod.accept-handler
35
 *
36
 * @abstract
37
 */
38
abstract class AbstractResponderLocator
39
{
40
41
    /**
42
     * Responder factories
43
     *
44
     * @var array
45
     *
46
     * @access protected
47
     */
48
    protected $factories = [];
49
50
    /**
51
     * Request
52
     *
53
     * @var Request
54
     *
55
     * @access protected
56
     */
57
    protected $request;
58
59
    /**
60
     * Response
61
     *
62
     * @var Response
63
     *
64
     * @access protected
65
     */
66
    protected $response;
67
68
    /**
69
     * Payload
70
     *
71
     * @var mixed
72
     *
73
     * @access protected
74
     */
75
    protected $payload;
76
77
    /**
78
     * Create a responder locator
79
     *
80
     * @param array $factories factories to create responders
81
     *
82
     * @access public
83
     */
84 5
    public function __construct(array $factories)
85
    {
86 5
        $this->factories = $factories;
87 5
    }
88
89
    /**
90
     * Negotiate and respond
91
     *
92
     * @param Request          $request  PSR7 Request
93
     * @param Response         $response PSR7 Response
94
     * @param PayloadInterface $payload  Domain Payload
95
     *
96
     * @return Response
97
     *
98
     * @access public
99
     */
100 4
    public function __invoke(
101
        Request $request,
102
        Response $response,
103
        $payload = null
104
    ) {
105 4
        $this->request = $request;
106 4
        $this->response = $response;
107 4
        $this->payload = $payload;
108
109 4
        $type = $this->negotiate($request);
110
111 4
        if (! $type || ! $this->has($type)) {
112 1
            return $this->notAcceptable();
113
        }
114
115 3
        $responder = $this->get($type);
116
117 3
        return $responder($request, $response, $payload);
118
    }
119
120
    /**
121
     * Accepts
122
     *
123
     * @return array
124
     *
125
     * @access public
126
     */
127 2
    public function accepts()
128
    {
129 2
        return array_keys($this->factories);
130
    }
131
132
    /**
133
     * Unavailable
134
     *
135
     * @return Response
136
     *
137
     * @access protected
138
     */
139 1
    protected function notAcceptable()
140
    {
141 1
        $this->response = $this->response->withStatus(406)
142 1
            ->withHeader('Content-Type', 'application/json');
143 1
        $this->response->getBody()->write(json_encode($this->accepts()));
144 1
        return $this->response;
145
    }
146
147
    /**
148
     * Set
149
     *
150
     * @param string  $name     name/type of responder
151
     * @param calable $callable factory
152
     *
153
     * @return $this
154
     *
155
     * @access public
156
     */
157 1
    public function set($name, callable $callable)
158
    {
159 1
        $this->factories[$name] = $callable;
160 1
        return $this;
161
    }
162
    /**
163
     * Does a named helper exist?
164
     *
165
     * @param string $name The responder name.
166
     *
167
     * @return bool
168
     */
169 5
    public function has($name)
170
    {
171 5
        return isset($this->factories[$name]);
172
    }
173
174
    /**
175
     * Get a responder
176
     *
177
     * @param string $name The responder to retrieve.
178
     *
179
     * @return object
180
     */
181 4
    public function get($name)
182
    {
183 4
        if (! $this->has($name)) {
184 1
            throw new Exception\ResponderNotFoundException(
185 1
                'Responder not found for: ' . $name
186
            );
187
        }
188
189 3
        $factory = $this->factories[$name];
190 3
        return $factory();
191
    }
192
193
    /**
194
     * Negotiate
195
     *
196
     * @param Request $request PSR7 Request
197
     *
198
     * @return string | false
199
     *
200
     * @access protected
201
     */
202
    abstract protected function negotiate(Request $request);
203
}
204