Skip to content

Commit ebbfd11

Browse files
Copilotjbtronics
andcommitted
Extract entity functionality into traits and interfaces
Co-authored-by: jbtronics <[email protected]>
1 parent dcafdbe commit ebbfd11

14 files changed

+1084
-740
lines changed

src/Entity/Attachments/AttachmentContainingDBElement.php

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424

2525
use App\Entity\Base\AbstractNamedDBElement;
2626
use App\Entity\Base\MasterAttachmentTrait;
27+
use App\Entity\Base\AttachmentsTrait;
2728
use App\Entity\Contracts\HasAttachmentsInterface;
2829
use App\Entity\Contracts\HasMasterAttachmentInterface;
2930
use App\Repository\AttachmentContainingDBElementRepository;
30-
use Doctrine\Common\Collections\ArrayCollection;
31-
use Doctrine\Common\Collections\Collection;
3231
use Doctrine\ORM\Mapping as ORM;
33-
use Symfony\Component\Serializer\Annotation\Groups;
3432

3533
/**
3634
* @template AT of Attachment
@@ -39,83 +37,18 @@
3937
abstract class AttachmentContainingDBElement extends AbstractNamedDBElement implements HasMasterAttachmentInterface, HasAttachmentsInterface
4038
{
4139
use MasterAttachmentTrait;
42-
43-
/**
44-
* @var Collection<int, Attachment>
45-
* @phpstan-var Collection<int, AT>
46-
* ORM Mapping is done in subclasses (e.g. Part)
47-
*/
48-
#[Groups(['full', 'import'])]
49-
protected Collection $attachments;
40+
use AttachmentsTrait;
5041

5142
public function __construct()
5243
{
53-
$this->attachments = new ArrayCollection();
44+
$this->initializeAttachments();
5445
}
5546

5647
public function __clone()
5748
{
58-
if ($this->id) {
59-
$attachments = $this->attachments;
60-
$this->attachments = new ArrayCollection();
61-
//Set master attachment is needed
62-
foreach ($attachments as $attachment) {
63-
$clone = clone $attachment;
64-
if ($attachment === $this->master_picture_attachment) {
65-
$this->setMasterPictureAttachment($clone);
66-
}
67-
$this->addAttachment($clone);
68-
}
69-
}
49+
$this->cloneAttachments();
7050

7151
//Parent has to be last call, as it resets the ID
7252
parent::__clone();
7353
}
74-
75-
/********************************************************************************
76-
*
77-
* Getters
78-
*
79-
*********************************************************************************/
80-
81-
/**
82-
* Gets all attachments associated with this element.
83-
*/
84-
public function getAttachments(): Collection
85-
{
86-
return $this->attachments;
87-
}
88-
89-
/**
90-
* Adds an attachment to this element.
91-
*
92-
* @param Attachment $attachment Attachment
93-
*
94-
* @return $this
95-
*/
96-
public function addAttachment(Attachment $attachment): self
97-
{
98-
//Attachment must be associated with this element
99-
$attachment->setElement($this);
100-
$this->attachments->add($attachment);
101-
102-
return $this;
103-
}
104-
105-
/**
106-
* Removes the given attachment from this element.
107-
*
108-
* @return $this
109-
*/
110-
public function removeAttachment(Attachment $attachment): self
111-
{
112-
$this->attachments->removeElement($attachment);
113-
114-
//Check if this is the master attachment -> remove it from master attachment too, or it can not be deleted from DB...
115-
if ($attachment === $this->getMasterPictureAttachment()) {
116-
$this->setMasterPictureAttachment(null);
117-
}
118-
119-
return $this;
120-
}
12154
}

src/Entity/Base/AbstractCompany.php

Lines changed: 4 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424

2525
use App\Entity\Attachments\Attachment;
2626
use App\Entity\Parameters\AbstractParameter;
27-
use Doctrine\DBAL\Types\Types;
27+
use App\Entity\Contracts\CompanyInterface;
2828
use Doctrine\ORM\Mapping as ORM;
2929
use Symfony\Component\Serializer\Annotation\Groups;
30-
use function is_string;
31-
use Symfony\Component\Validator\Constraints as Assert;
3230

3331
/**
3432
* This abstract class is used for companies like suppliers or manufacturers.
@@ -38,226 +36,15 @@
3836
* @extends AbstractPartsContainingDBElement<AT, PT>
3937
*/
4038
#[ORM\MappedSuperclass]
41-
abstract class AbstractCompany extends AbstractPartsContainingDBElement
39+
abstract class AbstractCompany extends AbstractPartsContainingDBElement implements CompanyInterface
4240
{
41+
use CompanyTrait;
42+
4343
#[Groups(['company:read'])]
4444
protected ?\DateTimeImmutable $addedDate = null;
4545
#[Groups(['company:read'])]
4646
protected ?\DateTimeImmutable $lastModified = null;
4747

48-
/**
49-
* @var string The address of the company
50-
*/
51-
#[Groups(['full', 'company:read', 'company:write', 'import', 'extended'])]
52-
#[ORM\Column(type: Types::STRING)]
53-
#[Assert\Length(max: 255)]
54-
protected string $address = '';
55-
56-
/**
57-
* @var string The phone number of the company
58-
*/
59-
#[Groups(['full', 'company:read', 'company:write', 'import', 'extended'])]
60-
#[ORM\Column(type: Types::STRING)]
61-
#[Assert\Length(max: 255)]
62-
protected string $phone_number = '';
63-
64-
/**
65-
* @var string The fax number of the company
66-
*/
67-
#[Groups(['full', 'company:read', 'company:write', 'import', 'extended'])]
68-
#[ORM\Column(type: Types::STRING)]
69-
#[Assert\Length(max: 255)]
70-
protected string $fax_number = '';
71-
72-
/**
73-
* @var string The email address of the company
74-
*/
75-
#[Assert\Email]
76-
#[Groups(['full', 'company:read', 'company:write', 'import', 'extended'])]
77-
#[ORM\Column(type: Types::STRING)]
78-
#[Assert\Length(max: 255)]
79-
protected string $email_address = '';
80-
81-
/**
82-
* @var string The website of the company
83-
*/
84-
#[Assert\Url(requireTld: false)]
85-
#[Groups(['full', 'company:read', 'company:write', 'import', 'extended'])]
86-
#[ORM\Column(type: Types::STRING, length: 2048)]
87-
#[Assert\Length(max: 2048)]
88-
protected string $website = '';
89-
9048
#[Groups(['company:read', 'company:write', 'import', 'full', 'extended'])]
9149
protected string $comment = '';
92-
93-
/**
94-
* @var string The link to the website of an article. Use %PARTNUMBER% as placeholder for the part number.
95-
*/
96-
#[ORM\Column(type: Types::STRING, length: 2048)]
97-
#[Assert\Length(max: 2048)]
98-
#[Groups(['full', 'company:read', 'company:write', 'import', 'extended'])]
99-
protected string $auto_product_url = '';
100-
101-
/********************************************************************************
102-
*
103-
* Getters
104-
*
105-
*********************************************************************************/
106-
107-
/**
108-
* Get the address.
109-
*
110-
* @return string the address of the company (with "\n" as line break)
111-
*/
112-
public function getAddress(): string
113-
{
114-
return $this->address;
115-
}
116-
117-
/**
118-
* Get the phone number.
119-
*
120-
* @return string the phone number of the company
121-
*/
122-
public function getPhoneNumber(): string
123-
{
124-
return $this->phone_number;
125-
}
126-
127-
/**
128-
* Get the fax number.
129-
*
130-
* @return string the fax number of the company
131-
*/
132-
public function getFaxNumber(): string
133-
{
134-
return $this->fax_number;
135-
}
136-
137-
/**
138-
* Get the e-mail address.
139-
*
140-
* @return string the e-mail address of the company
141-
*/
142-
public function getEmailAddress(): string
143-
{
144-
return $this->email_address;
145-
}
146-
147-
/**
148-
* Get the website.
149-
*
150-
* @return string the website of the company
151-
*/
152-
public function getWebsite(): string
153-
{
154-
return $this->website;
155-
}
156-
157-
/**
158-
* Get the link to the website of an article.
159-
*
160-
* @param string|null $partnr * NULL for returning the URL with a placeholder for the part number
161-
* * or the part number for returning the direct URL to the article
162-
*
163-
* @return string the link to the article
164-
*/
165-
public function getAutoProductUrl(?string $partnr = null): string
166-
{
167-
if (is_string($partnr)) {
168-
return str_replace('%PARTNUMBER%', $partnr, $this->auto_product_url);
169-
}
170-
171-
return $this->auto_product_url;
172-
}
173-
174-
/********************************************************************************
175-
*
176-
* Setters
177-
*
178-
*********************************************************************************/
179-
180-
/**
181-
* Set the addres.
182-
*
183-
* @param string $new_address the new address (with "\n" as line break)
184-
*
185-
* @return $this
186-
*/
187-
public function setAddress(string $new_address): self
188-
{
189-
$this->address = $new_address;
190-
191-
return $this;
192-
}
193-
194-
/**
195-
* Set the phone number.
196-
*
197-
* @param string $new_phone_number the new phone number
198-
*
199-
* @return $this
200-
*/
201-
public function setPhoneNumber(string $new_phone_number): self
202-
{
203-
$this->phone_number = $new_phone_number;
204-
205-
return $this;
206-
}
207-
208-
/**
209-
* Set the fax number.
210-
*
211-
* @param string $new_fax_number the new fax number
212-
*
213-
* @return $this
214-
*/
215-
public function setFaxNumber(string $new_fax_number): self
216-
{
217-
$this->fax_number = $new_fax_number;
218-
219-
return $this;
220-
}
221-
222-
/**
223-
* Set the e-mail address.
224-
*
225-
* @param string $new_email_address the new e-mail address
226-
*
227-
* @return $this
228-
*/
229-
public function setEmailAddress(string $new_email_address): self
230-
{
231-
$this->email_address = $new_email_address;
232-
233-
return $this;
234-
}
235-
236-
/**
237-
* Set the website.
238-
*
239-
* @param string $new_website the new website
240-
*
241-
* @return $this
242-
*/
243-
public function setWebsite(string $new_website): self
244-
{
245-
$this->website = $new_website;
246-
247-
return $this;
248-
}
249-
250-
/**
251-
* Set the link to the website of an article.
252-
*
253-
* @param string $new_url the new URL with the placeholder %PARTNUMBER% for the part number
254-
*
255-
* @return $this
256-
*/
257-
public function setAutoProductUrl(string $new_url): self
258-
{
259-
$this->auto_product_url = $new_url;
260-
261-
return $this;
262-
}
26350
}

0 commit comments

Comments
 (0)