Completed
Push — master ( 04c44f...f6f21a )
by Oscar
02:41
created

Admin::getEntityId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
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\ServerMiddleware\MiddlewareInterface;
10
use Interop\Http\ServerMiddleware\DelegateInterface;
11
use Zend\Diactoros\Response;
12
use Relay\RelayBuilder;
13
14
/**
15
 * Main manager.
16
 */
17
class Admin extends App implements MiddlewareInterface
18
{
19
    private $entities = [];
20
21
    public $title = 'Folk';
22
    public $description = 'Universal CMS';
23
24
    public function __construct($path, UriInterface $uri)
25
    {
26
        parent::__construct($path, $uri);
27
28
        $this->addServiceProvider(new Providers\Formats());
29
        $this->addServiceProvider(new Providers\Middleware());
30
        $this->addServiceProvider(new Providers\Router());
31
        $this->addServiceProvider(new Providers\Templates());
32
    }
33
34
    /**
35
     * Use the app as a middleware component.
36
     *
37
     * @param ServerRequestInterface $request
38
     * @param DelegateInterface      $delegate
39
     *
40
     * @return ResponseInterface
41
     */
42
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
43
    {
44
        return $this->__invoke($request);
45
    }
46
47
    /**
48
     * @return ResponseInterface
49
     */
50
    public function __invoke(ServerRequestInterface $request): ResponseInterface
51
    {
52
        $dispatcher = $this->get('middleware');
53
54
        return $dispatcher->dispatch($request);
55
    }
56
57
    public function getRoute(string $name, array $data = [], array $query = null): string
58
    {
59
        return $this->getUri($this->get('router')->getGenerator()->generate($name, $data)).($query ? '?'.http_build_query($query) : '');
60
    }
61
62
    /**
63
     * Set the admin entities.
64
     *
65
     * @param array $entities
66
     */
67
    public function setEntities(array $entities)
68
    {
69
        foreach ($entities as $name => $entity) {
70
            if (is_int($name)) {
71
                $name = substr(strrchr($entity, '\\'), 1);
72
            }
73
74
            $this->addEntity(new $entity(strtolower($name), $this));
75
        }
76
    }
77
78
    /**
79
     * Add a new entity.
80
     *
81
     * @param EntityInterface $entity
82
     * @param string|null $id
83
     */
84
    public function addEntity(EntityInterface $entity, $id = null)
85
    {
86
        $name = $entity->getName();
87
88
        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...
89
            $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...
90
        }
91
92
        $this->entities[$name] = [$entity, $id];
93
    }
94
95
    /**
96
     * Return whether an entity exists.
97
     *
98
     * @param string $name
99
     *
100
     * @return bool
101
     */
102
    public function hasEntity(string $name): bool
103
    {
104
        return isset($this->entities[$name]);
105
    }
106
107
    /**
108
     * Return the entity id.
109
     *
110
     * @param string $name
111
     *
112
     * @return mixed|null
113
     */
114
    public function getEntityId(string $name)
115
    {
116
        if ($this->hasEntity($name)) {
117
            return $this->entities[$name][1];
118
        }
119
    }
120
121
    /**
122
     * Return an entity.
123
     *
124
     * @param string $name
125
     *
126
     * @throw NotFoundException
127
     * 
128
     * @return EntityInterface
129
     */
130
    public function getEntity(string $name): EntityInterface
131
    {
132
        if ($this->hasEntity($name)) {
133
            return $this->entities[$name][0];
134
        }
135
136
        throw new NotFoundException(sprintf('Entity %s not found', $name));
137
    }
138
139
    /**
140
     * Return all entities.
141
     *
142
     * @return array
143
     */
144
    public function getAllEntities(): array
145
    {
146
        return $this->entities;
147
    }
148
}
149