Passed
Push — master ( d5add8...c58f02 )
by Luiz Kim
02:26
created

ProductService::updateProductInventory()   A

Complexity

Conditions 2
Paths 10

Size

Total Lines 66
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 66
rs 9.6333
cc 2
nc 10
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ControleOnline\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\Security\Core\Security;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Core\Security was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Doctrine\ORM\QueryBuilder;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\QueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class ProductService
10
{
11
    public function __construct(
12
        private EntityManagerInterface $manager,
13
        private Security $security,
14
        private PeopleService $PeopleService
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PeopleService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
    ) {}
16
17
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $queryBuilder is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
    public function securityFilter(/** @scrutinizer ignore-unused */ QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $rootAlias is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, /** @scrutinizer ignore-unused */ $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resourceClass is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
    public function securityFilter(QueryBuilder $queryBuilder, /** @scrutinizer ignore-unused */ $resourceClass = null, $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $applyTo is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

17
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, /** @scrutinizer ignore-unused */ $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
    {
19
        // $this->PeopleService->checkCompany('company', $queryBuilder, $resourceClass, $applyTo, $rootAlias);
20
    }
21
22
    public function updateProductInventory(): void
23
    {
24
25
        $sql = "INSERT INTO `product_inventory` (
26
                `inventory_id`, 
27
                `product_id`, 
28
                `product_unity_id`, 
29
                `available`, 
30
                `ordered`, 
31
                `transit`, 
32
                `minimum`, 
33
                `maximum`, 
34
                `sales`
35
            )
36
            SELECT 
37
                op.`inventory_id`,
38
                op.`product_id`,
39
                p.`product_unity_id`,
40
                (SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:purchasing_status) THEN op.`quantity` ELSE 0 END) - 
41
                 SUM(CASE WHEN o.`order_type` = 'sale' AND o.`status_id` IN (:sales_status) THEN op.`quantity` ELSE 0 END)) AS `available`,
42
                SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:ordered_status) THEN op.`quantity` ELSE 0 END) AS `ordered`,
43
                SUM(CASE WHEN o.`order_type` = 'purchasing' AND o.`status_id` IN (:transit_status) THEN op.`quantity` ELSE 0 END) AS `transit`,
44
                0 AS `minimum`,
45
                0 AS `maximum`,
46
                SUM(CASE WHEN o.`order_type` = 'sale' AND o.`status_id` IN (:sales_status) THEN op.`quantity` ELSE 0 END) AS `sales`
47
            FROM `orders` o
48
            JOIN `order_product` op ON o.`id` = op.`order_id`
49
            JOIN `product` p ON op.`product_id` = p.`id`
50
            WHERE o.`order_type` IN ('purchasing', 'sale')
51
              AND o.`status_id` IN (:all_status)
52
              AND p.`type` IN ('product', 'feedstock')
53
              AND (
54
                  (o.`order_type` = 'sale' AND o.`provider_id` IN (:provider_id))
55
                  OR
56
                  (o.`order_type` = 'purchasing' AND o.`client_id` IN (:client_id))
57
              )
58
            GROUP BY op.`inventory_id`, op.`product_id`, p.`product_unity_id`
59
            ON DUPLICATE KEY UPDATE
60
                `available` = VALUES(`available`),
61
                `ordered` = VALUES(`ordered`),
62
                `transit` = VALUES(`transit`),
63
                `sales` = VALUES(`sales`)
64
        ";
65
66
67
        $purchasingStatus = [7];
68
        $orderedStatus = [5];
69
        $transitStatus = [6];
70
        $salesStatus = [6];
71
        $companies = [1, 2, 3];
72
        $allStatus = array_unique(array_merge($purchasingStatus, $orderedStatus, $transitStatus, $salesStatus));
73
74
        try {
75
            $stmt = $this->manager->getConnection()->prepare($sql);
76
77
            $stmt->bindValue('purchasing_status', implode(',', $purchasingStatus), \PDO::PARAM_STR);
78
            $stmt->bindValue('sales_status', implode(',', $salesStatus), \PDO::PARAM_STR);
79
            $stmt->bindValue('ordered_status', implode(',', $orderedStatus), \PDO::PARAM_STR);
80
            $stmt->bindValue('transit_status', implode(',', $transitStatus), \PDO::PARAM_STR);
81
            $stmt->bindValue('all_status', implode(',', $allStatus), \PDO::PARAM_STR);
82
            $stmt->bindValue('provider_id', implode(',', $companies), \PDO::PARAM_STR);
83
            $stmt->bindValue('client_id', implode(',', $companies), \PDO::PARAM_STR);
84
85
            $stmt->executeQuery();
86
        } catch (\Exception $e) {
87
            throw new \Exception("Erro ao atualizar o estoque: " . $e->getMessage());
88
        }
89
    }
90
}
91