Passed
Branch master (350f1b)
by Jan
04:53
created

Part::getDevices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Attachments\PartAttachment;
56
use App\Entity\Devices\Device;
57
use App\Entity\Parameters\ParametersTrait;
58
use App\Entity\Parameters\PartParameter;
59
use App\Entity\Parts\PartTraits\AdvancedPropertyTrait;
60
use App\Entity\Parts\PartTraits\BasicPropertyTrait;
61
use App\Entity\Parts\PartTraits\InstockTrait;
62
use App\Entity\Parts\PartTraits\ManufacturerTrait;
63
use App\Entity\Parts\PartTraits\OrderTrait;
64
use App\Security\Annotations\ColumnSecurity;
65
use DateTime;
66
use Doctrine\Common\Collections\ArrayCollection;
67
use Doctrine\Common\Collections\Collection;
68
use Doctrine\ORM\Mapping as ORM;
69
use Symfony\Component\Validator\Constraints as Assert;
70
71
/**
72
 * Part class.
73
 *
74
 * The class properties are split over various traits in directory PartTraits.
75
 * Otherwise this class would be too big, to be maintained.
76
 *
77
 * @ORM\Entity(repositoryClass="App\Repository\PartRepository")
78
 * @ORM\Table("`parts`")
79
 */
80
class Part extends AttachmentContainingDBElement
81
{
82
    use AdvancedPropertyTrait;
83
    //use MasterAttachmentTrait;
84
    use BasicPropertyTrait;
85
    use InstockTrait;
86
    use ManufacturerTrait;
87
    use OrderTrait;
88
    use ParametersTrait;
89
90
    /**
91
     * TODO.
92
     */
93
    protected $devices = [];
94
95
    /** @var Collection<int, PartParameter>
96
     * @Assert\Valid()
97
     * @ORM\OneToMany(targetEntity="App\Entity\Parameters\PartParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
98
     * @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"})
99
     */
100
    protected $parameters;
101
102
    /**
103
     * @ColumnSecurity(type="datetime")
104
     * @ORM\Column(type="datetime", name="datetime_added", options={"default"="CURRENT_TIMESTAMP"})
105
     */
106
    protected $addedDate;
107
108
    /** *************************************************************
109
     * Overridden properties
110
     * (They are defined here and not in a trait, to avoid conflicts).
111
     ****************************************************************/
112
113
    /**
114
     * @var string The name of this part
115
     * @ORM\Column(type="string")
116
     * @ColumnSecurity(prefix="name")
117
     */
118
    protected $name = '';
119
120
    /**
121
     * @var Collection<int, PartAttachment>
122
     * @ORM\OneToMany(targetEntity="App\Entity\Attachments\PartAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
123
     * @ColumnSecurity(type="collection", prefix="attachments")
124
     * @ORM\OrderBy({"name" = "ASC"})
125
     * @Assert\Valid()
126
     */
127
    protected $attachments;
128
129
    /**
130
     * @var DateTime the date when this element was modified the last time
131
     * @ColumnSecurity(type="datetime")
132
     * @ORM\Column(type="datetime", name="last_modified", options={"default"="CURRENT_TIMESTAMP"})
133
     */
134
    protected $lastModified;
135
136
    /**
137
     * @var Attachment
138
     * @ORM\ManyToOne(targetEntity="App\Entity\Attachments\Attachment")
139
     * @ORM\JoinColumn(name="id_preview_attachement", referencedColumnName="id")
140
     * @Assert\Expression("value == null or value.isPicture()", message="part.master_attachment.must_be_picture")
141
     */
142
    protected $master_picture_attachment;
143
144
    public function __construct()
145
    {
146
        parent::__construct();
147
        $this->partLots = new ArrayCollection();
148
        $this->orderdetails = new ArrayCollection();
149
        $this->parameters = new ArrayCollection();
150
    }
151
152
    public function __clone()
153
    {
154
        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...
155
            //Deep clone part lots
156
            $lots = $this->partLots;
157
            $this->partLots = new ArrayCollection();
158
            foreach ($lots as $lot) {
159
                $this->addPartLot(clone $lot);
160
            }
161
162
            //Deep clone order details
163
            $orderdetails = $this->orderdetails;
164
            $this->orderdetails = new ArrayCollection();
165
            foreach ($orderdetails as $orderdetail) {
166
                $this->addOrderdetail(clone $orderdetail);
167
            }
168
169
            //Deep clone parameters
170
            $parameters = $this->parameters;
171
            $this->parameters = new ArrayCollection();
172
            foreach ($parameters as $parameter) {
173
                $this->addParameter(clone $parameter);
174
            }
175
        }
176
        parent::__clone();
177
    }
178
179
180
    /**
181
     *  Get all devices which uses this part.
182
     *
183
     * @return Device[] * all devices which uses this part as a one-dimensional array of Device objects
184
     *                  (empty array if there are no ones)
185
     *                  * the array is sorted by the devices names
186
     */
187
    public function getDevices(): array
188
    {
189
        return $this->devices;
190
    }
191
}
192