Completed
Push — master ( 07dcbc...2d4def )
by Jan
04:20
created

PartPreviewGenerator::getTablePreviewAttachment()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 18
rs 10
c 0
b 0
f 0
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
     * Returns a list of attachments that can be used for previewing the part ordered by priority.
53
     * The priority is: Part MasterAttachment -> Footprint MasterAttachment -> Category MasterAttachment
54
     * -> Storelocation Attachment -> MeasurementUnit Attachment -> ManufacturerAttachment
55
     * @param Part $part The part for which the attachments should be determined.
56
     * @return Attachment[]
57
     */
58
    public function getPreviewAttachments(Part $part) : array
59
    {
60
        $list = [];
61
62
        //Master attachment has top priority
63
        $attachment = $part->getMasterPictureAttachment();
64
        if ($this->isAttachmentValidPicture($attachment)) {
65
            $list[] = $attachment;
66
        }
67
68
        if ($part->getFootprint() !== null) {
69
            $attachment = $part->getFootprint()->getMasterPictureAttachment();
70
            if ($this->isAttachmentValidPicture($attachment)) {
71
                $list[] = $attachment;
72
            }
73
        }
74
75
        if ($part->getCategory() !== null) {
76
            $attachment = $part->getCategory()->getMasterPictureAttachment();
77
            if ($this->isAttachmentValidPicture($attachment)) {
78
                $list[] = $attachment;
79
            }
80
        }
81
82
        foreach ($part->getPartLots() as $lot) {
83
            if ($lot->getStorageLocation() !== null) {
84
                $attachment = $lot->getStorageLocation()->getMasterPictureAttachment();
85
                if ($this->isAttachmentValidPicture($attachment)) {
86
                    $list[] = $attachment;
87
                }
88
            }
89
        }
90
91
        if ($part->getPartUnit() !== null) {
92
            $attachment = $part->getPartUnit()->getMasterPictureAttachment();
93
            if ($this->isAttachmentValidPicture($attachment)) {
94
                $list[] = $attachment;
95
            }
96
        }
97
98
        if ($part->getManufacturer() !== null) {
99
            $attachment = $part->getManufacturer()->getMasterPictureAttachment();
100
            if ($this->isAttachmentValidPicture($attachment)) {
101
                $list[] = $attachment;
102
            }
103
        }
104
105
        return $list;
106
    }
107
108
    /**
109
     * Determines what attachment should be used for previewing a part (especially in part table).
110
     * The returned attachment is guaranteed to be existing and be a picture.
111
     * @param Part $part The part for which the attachment should be determined
112
     * @return Attachment|null
113
     */
114
    public function getTablePreviewAttachment(Part $part) : ?Attachment
115
    {
116
        //First of all we check if the master attachment of the part is set (and a picture)
117
        $attachment = $part->getMasterPictureAttachment();
118
        if ($this->isAttachmentValidPicture($attachment)) {
119
            return $attachment;
120
        }
121
122
        //Otherwise check if the part has a footprint with a valid masterattachment
123
        if ($part->getFootprint() !== null) {
124
            $attachment = $part->getFootprint()->getMasterPictureAttachment();
125
            if ($this->isAttachmentValidPicture($attachment)) {
126
                return $attachment;
127
            }
128
        }
129
130
        //If nothing is available return null
131
        return null;
132
    }
133
134
    /**
135
     * Checks if a attachment is exising and a valid picture.
136
     * @param Attachment|null $attachment The attachment that should be checked.
137
     * @return bool True if the attachment is valid.
138
     */
139
    protected function isAttachmentValidPicture(?Attachment $attachment) : bool
140
    {
141
        return $attachment !== null
142
            && $attachment->isPicture()
143
            && $this->attachmentHelper->isFileExisting($attachment);
144
    }
145
}