Completed
Push — master ( 84d676...607d64 )
by Oscar
04:24
created

Admin::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Folk;
4
5
use Fol\{App, NotFoundException};
6
use Folk\Entities\EntityInterface;
7
use Folk\Entities\SingleEntityInterface;
8
use Psr\Http\Message\{ServerRequestInterface, ResponseInterface, UriInterface};
9
use Interop\Http\Server\RequestHandlerInterface;
10
use Zend\Diactoros\Response;
11
use Relay\RelayBuilder;
12
13
/**
14
 * Main manager.
15
 */
16
class Admin extends App implements RequestHandlerInterface
17
{
18
    private $entities = [];
19
20
    public $title = 'Folk';
21
    public $description = 'Universal CMS';
22
23
    public function __construct($path, UriInterface $uri)
24
    {
25
        parent::__construct($path, $uri);
26
27
        $this->addServiceProvider(new Providers\Formats());
28
        $this->addServiceProvider(new Providers\Middleware());
29
        $this->addServiceProvider(new Providers\Router());
30
        $this->addServiceProvider(new Providers\Templates());
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     * 
36
     * @return ResponseInterface
37
     */
38
    public function handle(ServerRequestInterface $request): ResponseInterface
39
    {
40
        $dispatcher = $this->get('middleware');
41
42
        return $dispatcher->dispatch($request);
43
    }
44
45
    public function getRoute(string $name, array $data = [], array $query = null): string
46
    {
47
        return $this->getUri($this->get('router')->getGenerator()->generate($name, $data)).($query ? '?'.http_build_query($query) : '');
48
    }
49
50
    /**
51
     * Set the admin entities.
52
     *
53
     * @param array $entities
54
     */
55
    public function setEntities(array $entities)
56
    {
57
        foreach ($entities as $name => $entity) {
58
            if (is_int($name)) {
59
                $name = substr(strrchr($entity, '\\'), 1);
60
            }
61
62
            $this->addEntity(new $entity(strtolower($name), $this));
63
        }
64
    }
65
66
    /**
67
     * Add a new entity.
68
     *
69
     * @param EntityInterface $entity
70
     * @param string|null $id
71
     */
72
    public function addEntity(EntityInterface $entity, $id = null)
73
    {
74
        $name = $entity->getName();
75
76
        if (empty($entity->title)) {
0 ignored issues
show
Bug introduced by
Accessing title on the interface Folk\Entities\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
77
            $entity->title = ucfirst($name);
0 ignored issues
show
Bug introduced by
Accessing title on the interface Folk\Entities\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
78
        }
79
80
        $this->entities[$name] = [$entity, $id];
81
    }
82
83
    /**
84
     * Return whether an entity exists.
85
     *
86
     * @param string $name
87
     *
88
     * @return bool
89
     */
90
    public function hasEntity(string $name): bool
91
    {
92
        return isset($this->entities[$name]);
93
    }
94
95
    /**
96
     * Return the entity id.
97
     *
98
     * @param string $name
99
     *
100
     * @return mixed|null
101
     */
102
    public function getEntityId(string $name)
103
    {
104
        if ($this->hasEntity($name)) {
105
            return $this->entities[$name][1];
106
        }
107
    }
108
109
    /**
110
     * Return an entity.
111
     *
112
     * @param string $name
113
     *
114
     * @throw NotFoundException
115
     * 
116
     * @return EntityInterface
117
     */
118
    public function getEntity(string $name): EntityInterface
119
    {
120
        if ($this->hasEntity($name)) {
121
            return $this->entities[$name][0];
122
        }
123
124
        throw new NotFoundException(sprintf('Entity %s not found', $name));
125
    }
126
127
    /**
128
     * Return all entities.
129
     *
130
     * @return array
131
     */
132
    public function getAllEntities(): array
133
    {
134
        return $this->entities;
135
    }
136
}
137