Completed
Push — extensions-support ( bcc5d6...dc7c75 )
by Patrick
03:13
created

AbstractRelation::getClassMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Relations;
4
5
use BadMethodCallException;
6
use Doctrine\Common\Inflector\Inflector;
7
use Doctrine\ORM\Mapping\Builder\AssociationBuilder;
8
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
9
use Doctrine\ORM\Mapping\NamingStrategy;
10
use InvalidArgumentException;
11
use LaravelDoctrine\Fluent\Builders\Traits\Queueable;
12
13
/**
14
 * @method $this orphanRemoval()
15
 * @method $this cascadeAll()
16
 * @method $this cascadePersist()
17
 * @method $this cascadeRemove()
18
 * @method $this cascadeMerge()
19
 * @method $this cascadeDetach()
20
 * @method $this cascadeRefresh()
21
 * @method $this fetchExtraLazy()
22
 * @method $this fetchEager()
23
 * @method $this fetchLazy()
24
 */
25
abstract class AbstractRelation implements Relation
26
{
27
    use Queueable {
28
        build as buildQueued;
29
    }
30
31
    /**
32
     * @var string
33
     */
34
    protected $relation;
35
36
    /**
37
     * @var string
38
     */
39
    protected $entity;
40
41
    /**
42
     * @var NamingStrategy
43
     */
44
    protected $namingStrategy;
45
46
    /**
47
     * @var ClassMetadataBuilder
48
     */
49
    protected $builder;
50
51
    /**
52
     * @var AssociationBuilder
53
     */
54
    protected $association;
55
56
    /**
57
     * @param ClassMetadataBuilder $builder
58
     * @param NamingStrategy       $namingStrategy
59
     * @param string               $relation
60
     * @param string               $entity
61
     */
62 125
    public function __construct(ClassMetadataBuilder $builder, NamingStrategy $namingStrategy, $relation, $entity)
63
    {
64 125
        $this->entity         = $entity;
65 125
        $this->builder        = $builder;
66 125
        $this->relation       = $relation;
67 125
        $this->namingStrategy = $namingStrategy;
68 125
        $this->association    = $this->createAssociation($builder, $relation, $entity);
69 125
    }
70
71
    /**
72
     * @param ClassMetadataBuilder $builder
73
     * @param string               $relation
74
     * @param string               $entity
75
     *
76
     * @return AssociationBuilder
77
     */
78
    abstract protected function createAssociation(ClassMetadataBuilder $builder, $relation, $entity);
79
80
    /**
81
     * @param string[] $cascade one of "persist", "remove", "merge", "detach", "refresh" or "ALL"
82
     *
83
     * @return $this
84
     */
85 12 View Code Duplication
    public function cascade(array $cascade)
86
    {
87 12
        foreach ($cascade as $name) {
88 12
            $method = 'cascade' . Inflector::classify(strtolower($name));
89
90 12
            if (!method_exists($this->association, $method)) {
91 4
                throw new InvalidArgumentException('Cascade [' . $name . '] does not exist');
92
            }
93
94 8
            $this->{$method}();
95 8
        }
96
97 8
        return $this;
98
    }
99
100
    /**
101
     * @param string $strategy one of "LAZY", "EAGER", "EXTRA_LAZY"
102
     *
103
     * @return $this
104
     */
105 8 View Code Duplication
    public function fetch($strategy)
106
    {
107 8
        $method = 'fetch' . Inflector::classify(strtolower($strategy));
108
109 8
        if (!method_exists($this->association, $method)) {
110 4
            throw new InvalidArgumentException('Fetch [' . $strategy . '] does not exist');
111
        }
112
113 4
        $this->{$method}();
114
115 4
        return $this;
116
    }
117
118
    /**
119
     * @return ClassMetadataBuilder
120
     */
121 63
    public function getBuilder()
122
    {
123 63
        return $this->builder;
124
    }
125
126
    /**
127
     * @return \Doctrine\ORM\Mapping\ClassMetadata|ExtensibleClassMetadata
128
     */
129 3
    public function getClassMetadata()
130
    {
131 3
        return $this->builder->getClassMetadata();
132
    }
133
134
    /**
135
     * @return AssociationBuilder
136
     */
137 91
    public function getAssociation()
138
    {
139 91
        return $this->association;
140
    }
141
142
    /**
143
     * @param string      $usage
144
     * @param string|null $region
145
     *
146
     * @return $this
147
     */
148 16
    public function cache($usage = 'READ_ONLY', $region = null)
149
    {
150 16
        $cache = new AssociationCache(
151 16
            $this->builder->getClassMetadata(),
152 16
            $this->relation,
153 16
            $usage,
154
            $region
155 16
        );
156
157 12
        $this->queue($cache);
158
159 12
        return $this;
160
    }
161
162
    /**
163
     * Execute the build process for all queued buildables
164
     */
165 86
    public function build()
166
    {
167 86
        $this->getAssociation()->build();
168
169 85
        $this->buildQueued();
170 85
    }
171
172
    /**
173
     * Magic call method works as a proxy for the Doctrine associationBuilder
174
     *
175
     * @param string $method
176
     * @param array  $args
177
     *
178
     * @throws BadMethodCallException
179
     * @return $this
180
     */
181 50
    public function __call($method, $args)
182
    {
183 50
        if (method_exists($this->getAssociation(), $method)) {
184 47
            call_user_func_array([$this->getAssociation(), $method], $args);
185
186 47
            return $this;
187
        }
188
189 4
        throw new BadMethodCallException("Relation method [{$method}] does not exist.");
190
    }
191
192
    /**
193
     * @return NamingStrategy
194
     */
195 61
    public function getNamingStrategy()
196
    {
197 61
        return $this->namingStrategy;
198
    }
199
200
    /**
201
     * @return string
202
     */
203 3
    public function getRelation()
204
    {
205 3
        return $this->relation;
206
    }
207
}
208