Completed
Push — master ( aedcf9...3a2a05 )
by Joschi
05:46 queued 03:04
created

ServerFacade   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.95%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 7
c 7
b 0
f 2
lcom 1
cbo 2
dl 0
loc 85
ccs 15
cts 19
cp 0.7895
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerRoute() 0 4 1
A dispatchRequest() 0 11 1
A getViewResources() 0 4 1
A enableObjectRoute() 0 4 1
A getServer() 0 7 2
A setViewResources() 0 4 1
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Ports
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\Ports\Facade;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Ports\Object;
41
use Apparat\Server\Infrastructure\Model\Server;
42
use Apparat\Server\Ports\Contract\RouteInterface;
43
use Apparat\Server\Ports\Types\ObjectRoute;
44
use Psr\Http\Message\ResponseInterface;
45
use Psr\Http\Message\ServerRequestInterface;
46
47
/**
48
 * Server facade
49
 *
50
 * @package Apparat\Server\Ports
51
 */
52
class ServerFacade
53
{
54
    /**
55
     * Server instance
56
     *
57
     * @var \Apparat\Server\Domain\Model\Server
58
     */
59
    protected static $server = null;
60
61
    /**
62
     * Register the default routes for a particular repository
63
     *
64
     * @param string $repositoryPath Repository path
65
     * @param int $enable Enable / disable default routes
66
     * @api
67
     */
68
    public static function enableObjectRoute($repositoryPath = '', $enable = ObjectRoute::ALL)
69
    {
70
        self::getServer()->enableObjectRoute($repositoryPath, $enable);
71
    }
72
73
    /**
74
     * Create and return the server instance
75
     *
76
     * @return Server Server instance
77
     */
78 71
    protected static function getServer()
79
    {
80 71
        if (self::$server === null) {
81
            self::$server = Kernel::create(Server::class);
82
        }
83 71
        return self::$server;
84
    }
85
86
    /**
87
     * Register a route
88
     *
89
     * @param RouteInterface $route
90
     * @api
91
     */
92 1
    public static function registerRoute(RouteInterface $route)
93
    {
94 1
        self::getServer()->registerRoute($route);
95 1
    }
96
97
    /**
98
     * Dispatch a request
99
     *
100
     * @param ServerRequestInterface $request
101
     * @return ResponseInterface $response
102
     * @api
103
     */
104 3
    public static function dispatchRequest(ServerRequestInterface $request)
105
    {
106
        // Dispatch the request to a route
107 3
        $route = self::getServer()->dispatchRequestToRoute($request);
108
109
        // Get the appropriate route action
110 3
        $action = self::getServer()->getRouteAction($request, $route);
111
112
        // Run the action response
113 3
        return $action();
114
    }
115
116
    /**
117
     * Set the view resources
118
     *
119
     * @param array $viewResources View resources
120
     */
121 1
    public static function setViewResources(array $viewResources)
122
    {
123 1
        self::getServer()->setViewResources($viewResources);
124 1
    }
125
126
    /**
127
     * Return view resources
128
     *
129
     * @param string|null $name Optional: view resource name
130
     * @return array View resources
131
     */
132 71
    public static function getViewResources($name = null)
133
    {
134 71
        return self::getServer()->getViewResources($name);
135
    }
136
}
137