Completed
Push — master ( 656ae7...a2f7b9 )
by Oscar
02:13
created

EntityFactory::addNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace SimpleCrud;
4
5
/**
6
 * Class to create instances of entities.
7
 */
8
class EntityFactory implements EntityFactoryInterface
9
{
10
    protected $db;
11
    protected $tables;
12
    protected $namespaces = [];
13
    protected $defaultEntity;
14
    protected $queryFactory;
15
    protected $fieldFactory;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param QueryFactory|null $queryFactory
21
     * @param FieldFactory|null $fieldFactory
22
     */
23
    public function __construct(QueryFactory $queryFactory = null, FieldFactory $fieldFactory = null)
24
    {
25
        $this->setQueryFactory($queryFactory ?: new QueryFactory());
26
        $this->setFieldFactory($fieldFactory ?: new FieldFactory());
27
    }
28
29
    /**
30
     * @see EntityFactoryInterface
31
     *
32
     * {@inheritdoc}
33
     */
34
    public function setDb(SimpleCrud $db)
35
    {
36
        $this->db = $db;
37
38
        return $this;
39
    }
40
41
    /**
42
     * Add a namespace for the entities classes.
43
     *
44
     * @param string $namespace
45
     *
46
     * @return self
47
     */
48
    public function addNamespace($namespace)
49
    {
50
        array_unshift($this->namespaces, $namespace);
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set the QueryFactory instance used by the entities.
57
     *
58
     * @param QueryFactory $queryFactory
59
     *
60
     * @return self
61
     */
62
    public function setQueryFactory(QueryFactory $queryFactory)
63
    {
64
        $this->queryFactory = $queryFactory;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Returns the QueryFactory instance used by the entities.
71
     *
72
     * @return QueryFactory
73
     */
74
    public function getQueryFactory()
75
    {
76
        return $this->queryFactory;
77
    }
78
79
    /**
80
     * Set the FieldFactory instance used by the entities.
81
     *
82
     * @param FieldFactory $fieldFactory
83
     *
84
     * @return self
85
     */
86
    public function setFieldFactory(FieldFactory $fieldFactory)
87
    {
88
        $this->fieldFactory = $fieldFactory;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Returns the FieldFactory instance used by the entities.
95
     *
96
     * @return FieldFactory
97
     */
98
    public function getFieldFactory()
99
    {
100
        return $this->fieldFactory;
101
    }
102
103
    /**
104
     * Set whether the entities are autocreated or not.
105
     *
106
     * @param string $defaultEntity Default class used by the entities
107
     *
108
     * @return self
109
     */
110
    public function setAutocreate($defaultEntity = 'SimpleCrud\\Entity')
111
    {
112
        $this->defaultEntity = $defaultEntity;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @see EntityFactoryInterface
119
     *
120
     * {@inheritdoc}
121
     */
122
    public function has($name)
123
    {
124
        if ($this->defaultEntity && in_array($name, $this->getTables())) {
125
            return true;
126
        }
127
128
        $ucname = ucfirst($name);
129
130 View Code Duplication
        foreach ($this->namespaces as $namespace) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
            $class = $namespace.$ucname;
132
133
            if (class_exists($class)) {
134
                return true;
135
            }
136
        }
137
    }
138
139
    /**
140
     * @see EntityFactoryInterface
141
     *
142
     * {@inheritdoc}
143
     */
144
    public function get($name)
145
    {
146
        try {
147
            $queryFactory = clone $this->queryFactory;
148
            $fieldFactory = clone $this->fieldFactory;
149
150
            $ucname = ucfirst($name);
151
152
            foreach ($this->namespaces as $namespace) {
153
                $class = $namespace.$ucname;
154
155
                if (class_exists($class)) {
156
                    return new $class($name, $this->db, $queryFactory, $fieldFactory);
157
                }
158
            }
159
160
            if ($this->defaultEntity && in_array($name, $this->getTables())) {
161
                $class = $this->defaultEntity;
162
163
                return new $class($name, $this->db, $queryFactory, $fieldFactory);
164
            }
165
        } catch (\Exception $exception) {
166
            throw new SimpleCrudException("Error getting the '{$name}' entity", 0, $exception);
167
        }
168
169
        throw new SimpleCrudException("Entity '{$name}' not found");
170
    }
171
172
    /**
173
     * Returns all tables in the database.
174
     *
175
     * @return array
176
     */
177
    private function getTables()
178
    {
179
        if ($this->tables === null) {
180
            $this->tables = $this->db->getTables();
181
        }
182
183
        return $this->tables;
184
    }
185
}
186