Failed Conditions
Pull Request — master (#7046)
by Gabriel
12:08
created

ORMInvalidArgumentException::entityIsRemoved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM;
6
7
use Doctrine\ORM\Mapping\AssociationMetadata;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
10
/**
11
 * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
12
 */
13
class ORMInvalidArgumentException extends \InvalidArgumentException
14
{
15
    /**
16
     * @param object $entity
17
     *
18
     * @return ORMInvalidArgumentException
19
     */
20 1
    public static function scheduleInsertForManagedEntity($entity)
21
    {
22 1
        return new self('A managed+dirty entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.');
23
    }
24
25
    /**
26
     * @param object $entity
27
     *
28
     * @return ORMInvalidArgumentException
29
     */
30 1
    public static function scheduleInsertForRemovedEntity($entity)
31
    {
32 1
        return new self('Removed entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.');
33
    }
34
35
    /**
36
     * @param object $entity
37
     *
38
     * @return ORMInvalidArgumentException
39
     */
40 1
    public static function scheduleInsertTwice($entity)
41
    {
42 1
        return new self('Entity ' . self::objToStr($entity) . ' can not be scheduled for insertion twice.');
43
    }
44
45
    /**
46
     * @param string $className
47
     * @param object $entity
48
     *
49
     * @return ORMInvalidArgumentException
50
     */
51 6
    public static function entityWithoutIdentity($className, $entity)
52
    {
53 6
        return new self(
54 6
            "The given entity of type '" . $className . "' (" . self::objToStr($entity) . ') has no identity/no ' .
55 6
            'id values set. It cannot be added to the identity map.'
56
        );
57
    }
58
59
    /**
60
     * @param object $entity
61
     *
62
     * @return ORMInvalidArgumentException
63
     */
64 1
    public static function readOnlyRequiresManagedEntity($entity)
65
    {
66 1
        return new self('Only managed entities can be marked or checked as read only. But ' . self::objToStr($entity) . ' is not');
67
    }
68
69
    /**
70
     * @param array[][]|object[][] $newEntitiesWithAssociations non-empty an array of [$associationMetadata, $entity] pairs
71
     *
72
     * @return ORMInvalidArgumentException
73
     */
74 4
    public static function newEntitiesFoundThroughRelationships($newEntitiesWithAssociations)
75
    {
76 4
        $errorMessages = array_map(
77 4
            function (array $newEntityWithAssociation) : string {
78 4
                [$associationMetadata, $entity] = $newEntityWithAssociation;
79
80 4
                return self::newEntityFoundThroughRelationshipMessage($associationMetadata, $entity);
81 4
            },
82 4
            $newEntitiesWithAssociations
83
        );
84
85 4
        if (count($errorMessages) === 1) {
86 4
            return new self(reset($errorMessages));
87
        }
88
89
        return new self(
90
            'Multiple non-persisted new entities were found through the given association graph:'
91
            . "\n\n * "
92
            . implode("\n * ", $errorMessages)
93
        );
94
    }
95
96
    /**
97
     * @param object $entry
98
     *
99
     * @return ORMInvalidArgumentException
100
     */
101
    public static function newEntityFoundThroughRelationship(AssociationMetadata $association, $entry)
102
    {
103
        $message = "A new entity was found through the relationship '%s#%s' that was not configured to cascade "
104
            . 'persist operations for entity: %s. To solve this issue: Either explicitly call EntityManager#persist() '
105
            . 'on this unknown entity or configure cascade persist this association in the mapping for example '
106
            . '@ManyToOne(..,cascade={"persist"}).%s';
107
108
        $messageAppend = method_exists($entry, '__toString')
109
            ? ''
110
            : " If you cannot find out which entity causes the problem implement '%s#__toString()' to get a clue."
111
        ;
112
113
        return new self(sprintf(
114
            $message,
115
            $association->getSourceEntity(),
116
            $association->getName(),
117
            self::objToStr($entry),
118
            sprintf($messageAppend, $association->getTargetEntity())
119
        ));
120
    }
121
122
    /**
123
     * @param object $entry
124
     *
125
     * @return ORMInvalidArgumentException
126
     */
127
    public static function detachedEntityFoundThroughRelationship(AssociationMetadata $association, $entry)
128
    {
129
        $messsage = "A detached entity of type %s (%s) was found through the relationship '%s#%s' during cascading a persist operation.";
130
131
        return new self(sprintf(
132
            $messsage,
133
            $association->getTargetEntity(),
134
            self::objToStr($entry),
135
            $association->getSourceEntity(),
136
            $association->getName()
137
        ));
138
    }
139
140
    /**
141
     * @param object $entity
142
     *
143
     * @return ORMInvalidArgumentException
144
     */
145 1
    public static function entityNotManaged($entity)
146
    {
147 1
        return new self('Entity ' . self::objToStr($entity) . ' is not managed. An entity is managed if its fetched ' .
148 1
            'from the database or registered as new through EntityManager#persist');
149
    }
150
151
    /**
152
     * @param object $entity
153
     * @param string $operation
154
     *
155
     * @return ORMInvalidArgumentException
156
     */
157
    public static function entityHasNoIdentity($entity, $operation)
158
    {
159
        return new self('Entity has no identity, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity));
160
    }
161
162
    /**
163
     * @param object $entity
164
     * @param string $operation
165
     *
166
     * @return ORMInvalidArgumentException
167
     */
168
    public static function entityIsRemoved($entity, $operation)
169
    {
170
        return new self('Entity is removed, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity));
171
    }
172
173
    /**
174
     * @param object $entity
175
     * @param string $operation
176
     *
177
     * @return ORMInvalidArgumentException
178
     */
179
    public static function detachedEntityCannot($entity, $operation)
180
    {
181
        return new self('Detached entity ' . self::objToStr($entity) . ' cannot be ' . $operation);
182
    }
183
184
    /**
185
     * @param string $context
186
     * @param mixed  $given
187
     * @param int    $parameterIndex
188
     *
189
     * @return ORMInvalidArgumentException
190
     */
191 3
    public static function invalidObject($context, $given, $parameterIndex = 1)
192
    {
193 3
        return new self($context . ' expects parameter ' . $parameterIndex .
194 3
            ' to be an entity object, ' . gettype($given) . ' given.');
195
    }
196
197
    /**
198
     * @return ORMInvalidArgumentException
199
     */
200
    public static function invalidCompositeIdentifier()
201
    {
202
        return new self('Binding an entity with a composite primary key to a query is not supported. ' .
203
            'You should split the parameter into the explicit fields and bind them separately.');
204
    }
205
206
    /**
207
     * @return ORMInvalidArgumentException
208
     */
209
    public static function invalidIdentifierBindingEntity()
210
    {
211
        return new self('Binding entities to query parameters only allowed for entities that have an identifier.');
212
    }
213
214
    /**
215
     * @param array $assoc
216
     * @param mixed $actualValue
217
     *
218
     * @return self
219
     */
220 15
    public static function invalidAssociation(ClassMetadata $targetClass, AssociationMetadata $association, $actualValue)
221
    {
222 15
        $expectedType = $targetClass->getClassName();
223
224 15
        return new self(sprintf(
225 15
            'Expected value of type "%s" for association field "%s#$%s", got "%s" instead.',
226 15
            $expectedType,
227 15
            $association->getSourceEntity(),
228 15
            $association->getName(),
229 15
            is_object($actualValue) ? get_class($actualValue) : gettype($actualValue)
230
        ));
231
    }
232
233
    /**
234
     * Helper method to show an object as string.
235
     *
236
     * @param object $obj
237
     */
238 15
    private static function objToStr($obj) : string
239
    {
240 15
        return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_id($obj);
241
    }
242
243
    /**
244
     * @param object $entity
245
     */
246 4
    private static function newEntityFoundThroughRelationshipMessage(AssociationMetadata $association, $entity) : string
247
    {
248
        return 'A new entity was found through the relationship \''
249 4
            . $association->getSourceEntity() . '#' . $association->getName() . '\' that was not'
250 4
            . ' configured to cascade persist operations for entity: ' . self::objToStr($entity) . '.'
251 4
            . ' To solve this issue: Either explicitly call EntityManager#persist()'
252 4
            . ' on this unknown entity or configure cascade persist'
253 4
            . ' this association in the mapping for example @ManyToOne(..,cascade={"persist"}).'
254 4
            . (method_exists($entity, '__toString')
255 1
                ? ''
256
                : ' If you cannot find out which entity causes the problem implement \''
257 4
                . $association->getTargetEntity() . '#__toString()\' to get a clue.'
258
            );
259
    }
260
}
261