Passed
Push — master ( ccdac1...a3c626 )
by Jan
04:20
created

AttachmentContainingDBElement::__clone()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 2
nop 0
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace App\Entity\Attachments;
24
25
use App\Entity\Base\MasterAttachmentTrait;
26
use App\Entity\Base\AbstractNamedDBElement;
27
use App\Entity\Contracts\HasAttachmentsInterface;
28
use App\Entity\Contracts\HasMasterAttachmentInterface;
29
use Doctrine\Common\Collections\ArrayCollection;
30
use Doctrine\Common\Collections\Collection;
31
use Doctrine\ORM\Mapping as ORM;
32
33
/**
34
 * @ORM\MappedSuperclass()
35
 */
36
abstract class AttachmentContainingDBElement extends AbstractNamedDBElement implements HasMasterAttachmentInterface, HasAttachmentsInterface
37
{
38
    use MasterAttachmentTrait;
39
40
    /**
41
     * @var Attachment[]|Collection
42
     *                   //TODO
43
     *                   //@ORM\OneToMany(targetEntity="Attachment", mappedBy="element")
44
     *
45
     * Mapping is done in sub classes like part
46
     */
47
    protected $attachments;
48
49
    public function __construct()
50
    {
51
        $this->attachments = new ArrayCollection();
52
    }
53
54
    /********************************************************************************
55
     *
56
     *   Getters
57
     *
58
     *********************************************************************************/
59
60
    /**
61
     * Gets all attachments associated with this element.
62
     *
63
     * @return Attachment[]|Collection
64
     */
65
    public function getAttachments(): Collection
66
    {
67
        return $this->attachments;
68
    }
69
70
    /**
71
     * Adds an attachment to this element.
72
     *
73
     * @param Attachment $attachment Attachment
74
     *
75
     * @return $this
76
     */
77
    public function addAttachment(Attachment $attachment): self
78
    {
79
        //Attachment must be associated with this element
80
        $attachment->setElement($this);
81
        $this->attachments->add($attachment);
82
83
        return $this;
84
    }
85
86
    /**
87
     * Removes the given attachment from this element.
88
     *
89
     * @param  Attachment  $attachment
90
     * @return $this
91
     */
92
    public function removeAttachment(Attachment $attachment): self
93
    {
94
        $this->attachments->removeElement($attachment);
95
96
        return $this;
97
    }
98
99
    public function __clone()
100
    {
101
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
102
            $attachments = $this->attachments;
103
            $this->attachments = new ArrayCollection();
104
            //Set master attachment is needed
105
            foreach ($attachments as $attachment) {
106
                $clone = clone $attachment;
107
                if ($attachment === $this->master_picture_attachment) {
108
                    $this->setMasterPictureAttachment($clone);
109
                }
110
                $this->addAttachment($clone);
111
            }
112
        }
113
114
        //Parent has to be last call, as it resets the ID
115
        parent::__clone();
116
    }
117
}
118