AcceptRequestAwareTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 93
ccs 15
cts 15
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setAcceptAttribute() 0 5 1
A negotiateMedia() 0 4 1
A negotiateCharset() 0 4 1
A negotiateLanguage() 0 4 1
A getAccept() 0 11 2
1
<?php
2
/**
3
 * Accept Handler
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  Trait
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;
21
22
use Psr\Http\Message\ResponseInterface as Response;
23
use Psr\Http\Message\ServerRequestInterface as Request;
24
25
use Aura\Accept\Accept;
26
27
/**
28
 * Accept Request aware trait
29
 *
30
 * Trait for objects which need to know where the accept attribute is stored in
31
 * the request.
32
 *
33
 * @category Trait
34
 * @package  Vperyod\AcceptHandler
35
 * @author   Jake Johns <[email protected]>
36
 * @license  http://jnj.mit-license.org/2016 MIT License
37
 * @link     https://github.com/vperyod/vperyod.accept-handler
38
 */
39
trait AcceptRequestAwareTrait
40
{
41
    /**
42
     * Attribute on request where accept is stored
43
     *
44
     * @var string
45
     *
46
     * @access protected
47
     */
48
    protected $acceptAttribute = 'aura/accept:accept';
49
50
    /**
51
     * Set accept attribute
52
     *
53
     * @param string $attr attribute on request where accept is stored
54
     *
55
     * @return $this
56
     *
57
     * @access public
58
     */
59 6
    public function setAcceptAttribute($attr)
60
    {
61 6
        $this->acceptAttribute = $attr;
62 6
        return $this;
63
    }
64
65
    /**
66
     * Get accept from request
67
     *
68
     * @param Request $request PSR7 Request
69
     *
70
     * @return Accept
71
     * @throws InvalidAcceptException if accept attribute is not an `Accept`
72
     *
73
     * @access protected
74
     */
75 6
    protected function getAccept(Request $request)
76
    {
77 6
        $accept = $request->getAttribute($this->acceptAttribute);
78 6
        if (! $accept instanceof Accept) {
79 1
            throw new Exception\InvalidAcceptException(
80
                'Accept not available in request at: '
81 1
                . $this->acceptAttribute
82
            );
83
        }
84 5
        return $accept;
85
    }
86
87
    /**
88
     * Negotiate media
89
     *
90
     * @param Request $request   PSR7 Request
91
     * @param array   $available array of available media types
92
     *
93
     * @return Aura\Accept\Media\MediaValue|false
94
     *
95
     * @access protected
96
     */
97 2
    protected function negotiateMedia(Request $request, array $available)
98
    {
99 2
        return $this->getAccept($request)->negotiateMedia($available);
100
    }
101
102
    /**
103
     * Negotiate charset
104
     *
105
     * @param Request $request   PSR7 Request
106
     * @param array   $available array of available charsets
107
     *
108
     * @return Aura\Accept\Charset\CharsetValue
109
     *
110
     * @access protected
111
     */
112 1
    protected function negotiateCharset(Request $request, array $available)
113
    {
114 1
        return $this->getAccept($request)->negotiateCharset($available);
115
    }
116
117
    /**
118
     * Negotiate language
119
     *
120
     * @param Request $request   PSR7 Request
121
     * @param array   $available arrayof available languages
122
     *
123
     * @return Aura\Accept\Language\LanguageValue
124
     *
125
     * @access protected
126
     */
127 1
    protected function negotiateLanguage(Request $request, array $available)
128
    {
129 1
        return $this->getAccept($request)->negotiateLanguage($available);
130
    }
131
}
132