1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Services\Attachments; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use App\Entity\Attachments\Attachment; |
36
|
|
|
use App\Entity\Parts\Part; |
37
|
|
|
use App\Services\AttachmentHelper; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @package App\Services\Attachments |
41
|
|
|
*/ |
42
|
|
|
class PartPreviewGenerator |
43
|
|
|
{ |
44
|
|
|
protected $attachmentHelper; |
45
|
|
|
|
46
|
|
|
public function __construct(AttachmentHelper $attachmentHelper) |
47
|
|
|
{ |
48
|
|
|
$this->attachmentHelper = $attachmentHelper; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Determines what attachment should be used for previewing a part (especially in part table). |
53
|
|
|
* The returned attachment is guaranteed to be existing and be a picture. |
54
|
|
|
* @param Part $part The part for which the attachment should be determined |
55
|
|
|
* @return Attachment|null |
56
|
|
|
*/ |
57
|
|
|
public function previewAttachment(Part $part) : ?Attachment |
58
|
|
|
{ |
59
|
|
|
//First of all we check if the master attachment of the part is set (and a picture) |
60
|
|
|
$attachment = $part->getMasterPictureAttachment(); |
61
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
62
|
|
|
return $attachment; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
//Otherwise check if the part has a footprint with a valid masterattachment |
66
|
|
|
if ($part->getFootprint() !== null) { |
67
|
|
|
$attachment = $part->getFootprint()->getMasterPictureAttachment(); |
68
|
|
|
if ($this->isAttachmentValidPicture($attachment)) { |
69
|
|
|
return $attachment; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//If nothing is available return null |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Checks if a attachment is exising and a valid picture. |
79
|
|
|
* @param Attachment|null $attachment The attachment that should be checked. |
80
|
|
|
* @return bool True if the attachment is valid. |
81
|
|
|
*/ |
82
|
|
|
protected function isAttachmentValidPicture(?Attachment $attachment) : bool |
83
|
|
|
{ |
84
|
|
|
return $attachment !== null |
85
|
|
|
&& $attachment->isPicture() |
86
|
|
|
&& $this->attachmentHelper->isFileExisting($attachment); |
87
|
|
|
} |
88
|
|
|
} |