Passed
Push — master ( 159572...890e9f )
by Jan
04:56 queued 10s
created

getUserUploadedAttachmentsCount()   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
 * 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
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (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 General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Services;
23
24
25
use App\Entity\Attachments\Attachment;
26
use App\Entity\Attachments\AttachmentType;
27
use App\Entity\Devices\Device;
28
use App\Entity\Parts\Category;
29
use App\Entity\Parts\Footprint;
30
use App\Entity\Parts\Manufacturer;
31
use App\Entity\Parts\MeasurementUnit;
32
use App\Entity\Parts\Part;
33
use App\Entity\Parts\Storelocation;
34
use App\Entity\Parts\Supplier;
35
use App\Entity\PriceInformations\Currency;
36
use Doctrine\ORM\EntityManagerInterface;
37
use Doctrine\ORM\EntityRepository;
38
39
class StatisticsHelper
40
{
41
    protected $em;
42
    protected $part_repo;
43
    protected $attachment_repo;
44
45
    public function __construct(EntityManagerInterface $em)
46
    {
47
        $this->em = $em;
48
        $this->part_repo = $this->em->getRepository(Part::class);
49
        $this->attachment_repo = $this->em->getRepository(Attachment::class);
50
    }
51
52
    /**
53
     *  Returns the count of distinct parts
54
     */
55
    public function getDistinctPartsCount(): int
56
    {
57
        return $this->part_repo->count([]);
58
    }
59
60
    /**
61
     * Returns the summed instocked over all parts (only parts without a measurement unit)
62
     * @return string
63
     * @throws \Doctrine\ORM\NoResultException
64
     * @throws \Doctrine\ORM\NonUniqueResultException
65
     */
66
    public function getPartsInstockSum(): string
67
    {
68
        return $this->part_repo->getPartsInstockSum();
69
    }
70
71
    /**
72
     * Returns the number of all parts which have price informations
73
     * @return int
74
     * @throws \Doctrine\ORM\NoResultException
75
     * @throws \Doctrine\ORM\NonUniqueResultException
76
     */
77
    public function getPartsCountWithPrice(): int
78
    {
79
        return $this->part_repo->getPartsCountWithPrice();
80
    }
81
82
    /**
83
     * Returns the number of datastructures for the given type.
84
     * @param  string  $type
85
     * @return int
86
     */
87
    public function getDataStructuresCount(string $type): int
88
    {
89
        $arr = [
90
            'attachment_type' => AttachmentType::class,
91
            'category' => Category::class,
92
            'device' => Device::class,
93
            'footprint' => Footprint::class,
94
            'manufacturer' => Manufacturer::class,
95
            'measurement_unit' => MeasurementUnit::class,
96
            'storelocation' => Storelocation::class,
97
            'supplier' => Supplier::class,
98
            'currency' => Currency::class,
99
        ];
100
101
        if (!isset($arr[$type])) {
102
            throw new \InvalidArgumentException('No count for the given type available!');
103
        }
104
105
        /** @var EntityRepository $repo */
106
        $repo = $this->em->getRepository($arr[$type]);
107
        return $repo->count([]);
108
    }
109
110
    /**
111
     * Gets the count of all attachments.
112
     * @return int
113
     */
114
    public function getAttachmentsCount(): int
115
    {
116
        return $this->attachment_repo->count([]);
117
    }
118
119
    /**
120
     * Gets the count of all private/secure attachments
121
     * @return int
122
     */
123
    public function getPrivateAttachmentsCount(): int
124
    {
125
        return $this->attachment_repo->getPrivateAttachmentsCount();
126
    }
127
128
    /**
129
     * Gets the count of all external (only containing an URL) attachments
130
     * @return int
131
     * @throws \Doctrine\ORM\NoResultException
132
     * @throws \Doctrine\ORM\NonUniqueResultException
133
     */
134
    public function getExternalAttachmentsCount(): int
135
    {
136
        return $this->attachment_repo->getExternalAttachments();
137
    }
138
139
    /**
140
     * Gets the count of all attachments where the user uploaded an file.
141
     * @return int
142
     * @throws \Doctrine\ORM\NoResultException
143
     * @throws \Doctrine\ORM\NonUniqueResultException
144
     */
145
    public function getUserUploadedAttachmentsCount(): int
146
    {
147
        return $this->attachment_repo->getUserUploadedAttachments();
148
    }
149
}