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

Part::__clone()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 0
dl 0
loc 18
rs 9.9332
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
/**
24
 * part-db version 0.1
25
 * Copyright (C) 2005 Christoph Lechner
26
 * http://www.cl-projects.de/.
27
 *
28
 * part-db version 0.2+
29
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
30
 * http://code.google.com/p/part-db/
31
 *
32
 * Part-DB Version 0.4+
33
 * Copyright (C) 2016 - 2019 Jan Böhmer
34
 * https://github.com/jbtronics
35
 *
36
 * This program is free software; you can redistribute it and/or
37
 * modify it under the terms of the GNU General Public License
38
 * as published by the Free Software Foundation; either version 2
39
 * of the License, or (at your option) any later version.
40
 *
41
 * This program is distributed in the hope that it will be useful,
42
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44
 * GNU General Public License for more details.
45
 *
46
 * You should have received a copy of the GNU General Public License
47
 * along with this program; if not, write to the Free Software
48
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
49
 */
50
51
namespace App\Entity\Parts;
52
53
use App\Entity\Attachments\Attachment;
54
use App\Entity\Attachments\AttachmentContainingDBElement;
55
use App\Entity\Devices\Device;
56
use App\Entity\Parts\PartTraits\AdvancedPropertyTrait;
57
use App\Entity\Parts\PartTraits\BasicPropertyTrait;
58
use App\Entity\Parts\PartTraits\InstockTrait;
59
use App\Entity\Parts\PartTraits\ManufacturerTrait;
60
use App\Entity\Parts\PartTraits\OrderTrait;
61
use App\Security\Annotations\ColumnSecurity;
62
use DateTime;
63
use Doctrine\Common\Collections\ArrayCollection;
64
use Doctrine\ORM\Mapping as ORM;
65
use Symfony\Component\Validator\Constraints as Assert;
66
67
/**
68
 * Part class.
69
 *
70
 * The class properties are split over various traits in directory PartTraits.
71
 * Otherwise this class would be too big, to be maintained.
72
 *
73
 * @ORM\Entity(repositoryClass="App\Repository\PartRepository")
74
 * @ORM\Table("`parts`")
75
 */
76
class Part extends AttachmentContainingDBElement
77
{
78
    use AdvancedPropertyTrait;
79
    //use MasterAttachmentTrait;
80
    use BasicPropertyTrait;
81
    use InstockTrait;
82
    use ManufacturerTrait;
83
    use OrderTrait;
84
85
    /**
86
     * TODO.
87
     */
88
    protected $devices = [];
89
90
    /**
91
     * @ColumnSecurity(type="datetime")
92
     * @ORM\Column(type="datetime", name="datetime_added", options={"default"="CURRENT_TIMESTAMP"})
93
     */
94
    protected $addedDate;
95
96
    /** *************************************************************
97
     * Overridden properties
98
     * (They are defined here and not in a trait, to avoid conflicts).
99
     ****************************************************************/
100
101
    /**
102
     * @var string The name of this part
103
     * @ORM\Column(type="string")
104
     * @ColumnSecurity(prefix="name")
105
     */
106
    protected $name = '';
107
108
    /**
109
     * @ORM\OneToMany(targetEntity="App\Entity\Attachments\PartAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
110
     * @ColumnSecurity(type="collection", prefix="attachments")
111
     * @Assert\Valid()
112
     */
113
    protected $attachments;
114
115
    /**
116
     * @var DateTime the date when this element was modified the last time
117
     * @ColumnSecurity(type="datetime")
118
     * @ORM\Column(type="datetime", name="last_modified", options={"default"="CURRENT_TIMESTAMP"})
119
     */
120
    protected $lastModified;
121
122
    /**
123
     * @var Attachment
124
     * @ORM\ManyToOne(targetEntity="App\Entity\Attachments\Attachment")
125
     * @ORM\JoinColumn(name="id_preview_attachement", referencedColumnName="id")
126
     * @Assert\Expression("value == null or value.isPicture()", message="part.master_attachment.must_be_picture")
127
     */
128
    protected $master_picture_attachment;
129
130
    public function __construct()
131
    {
132
        parent::__construct();
133
        $this->partLots = new ArrayCollection();
134
        $this->orderdetails = new ArrayCollection();
135
    }
136
137
    /**
138
     * Returns the ID as an string, defined by the element class.
139
     * This should have a form like P000014, for a part with ID 14.
140
     *
141
     * @return string The ID as a string;
142
     */
143
    public function getIDString(): string
144
    {
145
        return 'P'.sprintf('%06d', $this->getID());
146
    }
147
148
    /**
149
     *  Get all devices which uses this part.
150
     *
151
     * @return Device[] * all devices which uses this part as a one-dimensional array of Device objects
152
     *                  (empty array if there are no ones)
153
     *                  * the array is sorted by the devices names
154
     */
155
    public function getDevices(): array
156
    {
157
        return $this->devices;
158
    }
159
160
    public function __clone()
161
    {
162
        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...
163
            //Deep clone part lots
164
            $lots = $this->partLots;
165
            $this->partLots = new ArrayCollection();
166
            foreach ($lots as $lot) {
167
                $this->addPartLot(clone $lot);
168
            }
169
170
            //Deep clone order details
171
            $orderdetails = $this->orderdetails;
172
            $this->orderdetails = new ArrayCollection();
173
            foreach ($orderdetails as $orderdetail) {
174
                $this->addOrderdetail(clone $orderdetail);
175
            }
176
        }
177
        parent::__clone();
178
    }
179
}
180