Completed
Push — master ( b39b01...f31af2 )
by Oscar
08:50
created

EntityFactory::getQueryFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 $namespace;
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
     * Set the namespace for the entities classes.
43
     *
44
     * @param string $namespace
45
     *
46
     * @return self
47
     */
48
    public function setNamespace($namespace)
49
    {
50
        $this->namespace = $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(FieldFactory $fieldFactory)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldFactory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
        return ($this->defaultEntity && in_array($name, $this->getTables())) || class_exists($this->namespace.ucfirst($name));
125
    }
126
127
    /**
128
     * @see EntityFactoryInterface
129
     *
130
     * {@inheritdoc}
131
     */
132
    public function get($name)
133
    {
134
        try {
135
            $class = $this->namespace.ucfirst($name);
136
            $queryFactory = clone $this->queryFactory;
137
            $fieldFactory = clone $this->fieldFactory;
138
139
            if (class_exists($class)) {
140
                return new $class($name, $this->db, $queryFactory, $fieldFactory);
141
            }
142
143
            if ($this->defaultEntity && in_array($name, $this->getTables())) {
144
                $class = $this->defaultEntity;
145
146
                return new $class($name, $this->db, $queryFactory, $fieldFactory);
147
            }
148
        } catch (\Exception $exception) {
149
            throw new SimpleCrudException("Error getting the '{$name}' entity", 0, $exception);
150
        }
151
152
        throw new SimpleCrudException("Entity '{$name}' not found");
153
    }
154
155
    /**
156
     * Returns all tables in the database.
157
     *
158
     * @return array
159
     */
160
    private function getTables()
161
    {
162
        if ($this->tables === null) {
163
            $this->tables = $this->db->getTables();
164
        }
165
166
        return $this->tables;
167
    }
168
}
169