Completed
Push — master ( 5ea837...9dec3c )
by Paul
9s
created

MicroApp   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
A post() 0 4 1
A add() 0 8 1
A boot() 0 6 1
B dispatch() 0 31 2
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework;
12
13
use PPI\Framework\App as BaseApp;
14
use Symfony\Component\Routing\Matcher\UrlMatcher as UrlMatcher;
15
use Symfony\Component\Routing\Route as Route;
16
use Symfony\Component\Routing\RouteCollection;
17
use Symfony\Component\Routing\Router;
18
19
/**
20
 * The PPI MicroApp bootstrap class.
21
 *
22
 * This class sets various app settings, and allows you to override classes used in the bootup process.
23
 *
24
 * @author     Paul Dragoonis <[email protected]>
25
 */
26
class MicroApp extends BaseApp
27
{
28
    protected $router;
29
    protected $routes = array();
30
    protected $routeCollection;
31
32
    public function get($uri, $callback)
33
    {
34
        $this->add($uri, 'get', $callback);
35
    }
36
37
    public function post($uri, $callback)
38
    {
39
        $this->add($uri, 'post', $callback);
40
    }
41
42
    public function add($uri, $method, $callback)
43
    {
44
        $this->routes[md5($uri . $method)] = array(
45
            'method'   => $method,
46
            'uri'      => $uri,
47
            'callback' => $callback,
48
        );
49
    }
50
51
    public function boot()
52
    {
53
        parent::boot();
54
55
        return $this;
56
    }
57
58
    public function dispatch()
59
    {
60
61
        // @todo - create the RouteCollection
62
        // @todo - run the matcher against this and get a route back
63
64
        $this->router = $this->serviceManager->get('MicroRouter');
65
66
        $routeCollection = new RouteCollection();
67
        foreach ($this->routes as $routeKey => $r) {
68
            $route = new Route($r['uri']);
69
            $route->setMethods($r['method']);
70
71
            $routeCollection->add($routeKey, $route); // @todo - dont md5() this.
72
        }
73
74
        $request = $this->getRequest();
0 ignored issues
show
Bug introduced by
The method getRequest() does not seem to exist on object<PPI\Framework\MicroApp>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
76
        $requestContext = $this->getServiceManager()->get('RouterRequestContext');
77
        $requestContext->fromRequest($request);
78
79
        $matcher = new UrlMatcher($routeCollection, $requestContext);
0 ignored issues
show
Documentation introduced by
$requestContext is of type object|array, but the function expects a object<Symfony\Component\Routing\RequestContext>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
        // @todo - try catch this
81
        $routeAttributes = $matcher->match($request->getPathInfo());
82
        $matchedRouteKey = $routeAttributes['_route'];
83
84
        $this->routes[$matchedRouteKey]['callback']($this->getServiceManager());
85
86
        // @todo - handle when the callback returns a Response object and send that to the client.
87
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (PPI\Framework\MicroApp) is incompatible with the return type of the parent method PPI\Framework\App::dispatch of type PPI\Framework\Http\Response|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
88
    }
89
}
90