Passed
Pull Request — master (#78)
by
unknown
10:34
created

StatisticsHelper::getAttachmentsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7
 *
8
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as published
12
 * by the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace App\Services;
25
26
use App\Entity\Attachments\Attachment;
27
use App\Entity\Attachments\AttachmentType;
28
use App\Entity\Devices\Device;
29
use App\Entity\Parts\Category;
30
use App\Entity\Parts\Footprint;
31
use App\Entity\Parts\Manufacturer;
32
use App\Entity\Parts\MeasurementUnit;
33
use App\Entity\Parts\Part;
34
use App\Entity\Parts\Storelocation;
35
use App\Entity\Parts\Supplier;
36
use App\Entity\PriceInformations\Currency;
37
use Doctrine\ORM\EntityManagerInterface;
38
use Doctrine\ORM\EntityRepository;
39
40
class StatisticsHelper
41
{
42
    protected $em;
43
    protected $part_repo;
44
    protected $attachment_repo;
45
46
    public function __construct(EntityManagerInterface $em)
47
    {
48
        $this->em = $em;
49
        $this->part_repo = $this->em->getRepository(Part::class);
50
        $this->attachment_repo = $this->em->getRepository(Attachment::class);
51
    }
52
53
    /**
54
     *  Returns the count of distinct parts.
55
     */
56
    public function getDistinctPartsCount(): int
57
    {
58
        return $this->part_repo->count([]);
59
    }
60
61
    /**
62
     * Returns the summed instocked over all parts (only parts without a measurement unit).
63
     *
64
     * @throws \Doctrine\ORM\NoResultException
65
     * @throws \Doctrine\ORM\NonUniqueResultException
66
     */
67
    public function getPartsInstockSum(): float
68
    {
69
        return $this->part_repo->getPartsInstockSum();
70
    }
71
72
    /**
73
     * Returns the number of all parts which have price informations.
74
     *
75
     * @throws \Doctrine\ORM\NoResultException
76
     * @throws \Doctrine\ORM\NonUniqueResultException
77
     */
78
    public function getPartsCountWithPrice(): int
79
    {
80
        return $this->part_repo->getPartsCountWithPrice();
81
    }
82
83
    /**
84
     * Returns the number of all parts with low stock.
85
     *
86
     * @throws \Doctrine\ORM\NoResultException
87
     * @throws \Doctrine\ORM\NonUniqueResultException
88
     */
89
    public function getPartCountWithLowStock(): int
90
    {
91
        return $this->part_repo->getPartCountWithLowStock();
92
    }
93
94
    /**
95
     * Returns the number of datastructures for the given type.
96
     */
97
    public function getDataStructuresCount(string $type): int
98
    {
99
        $arr = [
100
            'attachment_type' => AttachmentType::class,
101
            'category' => Category::class,
102
            'device' => Device::class,
103
            'footprint' => Footprint::class,
104
            'manufacturer' => Manufacturer::class,
105
            'measurement_unit' => MeasurementUnit::class,
106
            'storelocation' => Storelocation::class,
107
            'supplier' => Supplier::class,
108
            'currency' => Currency::class,
109
        ];
110
111
        if (!isset($arr[$type])) {
112
            throw new \InvalidArgumentException('No count for the given type available!');
113
        }
114
115
        /** @var EntityRepository $repo */
116
        $repo = $this->em->getRepository($arr[$type]);
117
118
        return $repo->count([]);
119
    }
120
121
    /**
122
     * Gets the count of all attachments.
123
     */
124
    public function getAttachmentsCount(): int
125
    {
126
        return $this->attachment_repo->count([]);
127
    }
128
129
    /**
130
     * Gets the count of all private/secure attachments.
131
     */
132
    public function getPrivateAttachmentsCount(): int
133
    {
134
        return $this->attachment_repo->getPrivateAttachmentsCount();
135
    }
136
137
    /**
138
     * Gets the count of all external (only containing an URL) attachments.
139
     *
140
     * @throws \Doctrine\ORM\NoResultException
141
     * @throws \Doctrine\ORM\NonUniqueResultException
142
     */
143
    public function getExternalAttachmentsCount(): int
144
    {
145
        return $this->attachment_repo->getExternalAttachments();
146
    }
147
148
    /**
149
     * Gets the count of all attachments where the user uploaded an file.
150
     *
151
     * @throws \Doctrine\ORM\NoResultException
152
     * @throws \Doctrine\ORM\NonUniqueResultException
153
     */
154
    public function getUserUploadedAttachmentsCount(): int
155
    {
156
        return $this->attachment_repo->getUserUploadedAttachments();
157
    }
158
}
159