Completed
Push — master ( 5cd95a...6f73c5 )
by Oscar
03:28
created

Admin::getAllEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Folk;
4
5
use Fol;
6
use Folk\Entities\EntityInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Zend\Diactoros\Response;
10
use Relay\RelayBuilder;
11
12
/**
13
 * Main manager.
14
 */
15
class Admin extends Fol
16
{
17
    private $entities = [];
18
19
    public $title = 'Folk';
20
    public $description = 'Universal CMS';
21
22
    public function __construct($url)
23
    {
24
        $this->setUrl($url);
25
26
        $this->register(new Providers\Builder());
27
        $this->register(new Providers\Middlewares());
28
        $this->register(new Providers\Router());
29
        $this->register(new Providers\Templates());
30
    }
31
32
    /**
33
     * @return ResponseInterface
34
     */
35
    public function __invoke(ServerRequestInterface $request)
36
    {
37
        $dispatcher = (new RelayBuilder())->newInstance($this['middlewares']);
38
39
        return $dispatcher($request, new Response());
40
    }
41
42
    public function getRoute($name, array $data = array(), array $query = null)
43
    {
44
        return $this->getUrl($this['router']->getGenerator()->generate($name, $data)).($query ? '?'.http_build_query($query) : '');
45
    }
46
47
    /**
48
     * Set the admin entities.
49
     *
50
     * @param array $entities
51
     */
52
    public function setEntities(array $entities)
53
    {
54
        foreach ($entities as $name => $entity) {
55
            if (is_int($name)) {
56
                $name = strtolower(substr(strrchr($entity, '\\'), 1));
57
            }
58
59
            $this->addEntity(new $entity($name, $this));
60
        }
61
    }
62
63
    /**
64
     * Add a new entity.
65
     *
66
     * @param EntityInterface $entity
67
     */
68
    public function addEntity(EntityInterface $entity)
69
    {
70
        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...
71
            $entity->title = ucfirst($entity->getName());
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...
72
        }
73
74
        $this->entities[$entity->getName()] = $entity;
75
    }
76
77
    /**
78
     * Return an entity.
79
     *
80
     * @param string $name
81
     *
82
     * @return EntityInterface|null
83
     */
84
    public function getEntity($name)
85
    {
86
        return isset($this->entities[$name]) ? $this->entities[$name] : null;
87
    }
88
89
    /**
90
     * Return all entities.
91
     *
92
     * @return array
93
     */
94
    public function getAllEntities()
95
    {
96
        return $this->entities;
97
    }
98
}
99