AbstractRelation::cascade()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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