Completed
Push — master ( 058a41...742d39 )
by Joschi
03:39
created

ErrorService::explain()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 36
ccs 20
cts 20
cp 1
rs 8.439
cc 5
eloc 20
nc 5
nop 1
crap 5
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Infrastructure\Service
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Server\Infrastructure\Service;
38
39
use Apparat\Server\Domain\Contract\ObjectActionRouteInterface;
40
use Apparat\Server\Domain\Payload\PayloadInterface;
41
use Apparat\Server\Infrastructure\Route\ObjectPath;
42
use Apparat\Server\Ports\Service\AbstractService;
43
use Aura\Router\Rule\Accepts;
44
use Aura\Router\Rule\Allows;
45
46
/**
47
 * Error result service
48
 *
49
 * @package Apparat\Server
50
 * @subpackage Apparat\Server\Domain
51
 */
52
class ErrorService extends AbstractService
53
{
54
    /**
55
     * Explain a failed request with an appropriate error message
56
     *
57
     * @param array $attributes Request attributes
58
     * @return PayloadInterface Payload
59
     */
60 4
    public function explain(array $attributes)
61
    {
62
        // Which matching rule failed?
63 4
        switch ($attributes['failedRule']) {
64
            // Invalid path
65 4
            case ObjectPath::class:
66
                // If an object route failed
67 2
                $failedRouteReflection = new \ReflectionClass($attributes['failedRoute']);
68 2
                if ($failedRouteReflection->implementsInterface(ObjectActionRouteInterface::class)) {
69 1
                    return $this->payloadFactory->error(404, 'Bad apparat object request');
70
                }
71 1
                break;
72
73
            // Invalid method
74 2
            case Allows::class:
75 1
                return $this->payloadFactory->error(
76 1
                    405,
77 1
                    'Method not allowed',
78 1
                    ['Allow' => $attributes['allows']]
79
                );
80
81
            // Response not acceptable
82 1
            case Accepts::class:
83 1
                return $this->payloadFactory->error(
84 1
                    406,
85 1
                    'Not acceptable',
86 1
                    ['Accept' => $attributes['accept']]
87
                );
88
        }
89
90
        // Else: General error
91 1
        return $this->payloadFactory->error(
92 1
            404,
93 1
            'Not found'
94
        );
95
    }
96
}
97