Passed
Push — master ( 878c38...d4077c )
by Luiz Kim
02:25
created

OrderService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Order;
6
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People 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 ControleOnline\Entity\Status;
8
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...
9
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...
10
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...
11
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\RequestStack 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...
12
13
class OrderService
14
{
15
    private $request;
16
17
    public function __construct(
18
        private EntityManagerInterface $manager,
19
        private Security $security,
20
        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...
21
        RequestStack $requestStack
22
23
    ) {
24
        $this->request  = $requestStack->getCurrentRequest();
25
    }
26
27
    public function getPurchasingSuggestion(People $company)
28
    {
29
        return $this->manager->getRepository(Order::class)->getPurchasingSuggestion($company);
30
    }
31
32
    public function calculateOrderPrice(Order $order)
33
    {
34
        $sql = 'UPDATE orders O
35
                JOIN (
36
                    SELECT order_id, IFNULL(SUM(total), 0) AS new_total
37
                    FROM order_product
38
                    WHERE order_product_id IS NULL
39
                    GROUP BY order_id
40
                ) AS subquery ON O.id = subquery.order_id
41
                SET O.price = IFNULL(subquery.new_total, 0)
42
                WHERE O.id = :order_id;
43
                ';
44
        $connection = $this->manager->getConnection();
45
        $statement = $connection->prepare($sql);
46
        $statement->execute(['order_id' =>  $order->getId()]);
47
48
        return $order;
49
    }
50
51
52
    public function calculateGroupProductPrice(Order $order)
53
    {
54
        $sql = 'UPDATE order_product OPO
55
                JOIN (
56
                        SELECT SUM(calculated_price) AS calculated_price,order_product_id FROM (	SELECT 
57
                                PG.product_group,
58
                                P.product,
59
                                PG.price_calculation,
60
                                OP.order_product_id,
61
                                (CASE 
62
                                    WHEN PG.price_calculation = "biggest" THEN MAX(PGP.price)
63
                                    WHEN PG.price_calculation = "sum" THEN SUM(PGP.price)
64
                                    WHEN PG.price_calculation = "average" THEN AVG(PGP.price) 
65
                                    WHEN PG.price_calculation = "free" THEN 0
66
                                    ELSE NULL
67
                                END)  AS calculated_price
68
                                
69
                            FROM order_product OP
70
                            INNER JOIN product_group PG ON OP.product_group_id = PG.id
71
                            INNER JOIN product_group_product PGP ON PGP.product_group_id = OP.product_group_id AND PGP.product_child_id = OP.product_id
72
                            INNER JOIN product P ON P.id = OP.product_id
73
                            WHERE OP.parent_product_id IS NOT NULL AND OP.order_id = :order_id
74
                            GROUP BY OP.order_product_id,PG.id
75
                        ) AS SBG GROUP BY SBG.order_product_id
76
                        
77
                ) AS subquery ON OPO.id = subquery.order_product_id
78
                SET OPO.price = subquery.calculated_price,OPO.total = (subquery.calculated_price * OPO.quantity)
79
                ';
80
81
82
        $connection = $this->manager->getConnection();
83
        $statement = $connection->prepare($sql);
84
        $statement->execute(['order_id' =>  $order->getId()]);
85
        return $order;
86
    }
87
88
89
    public function createOrder(People $receiver, People $payer)
90
    {
91
        $status = $this->manager->getRepository(Status::class)->findOneBy([
92
            'status' => 'waiting payment',
93
            'context' => 'order'
94
        ]);
95
96
        $order = new Order();
97
        $order->setProvider($receiver);
98
        $order->setClient($payer);
99
        $order->setPayer($payer);
100
        $order->setOrderType('sale');
101
        $order->setStatus($status);
102
        $order->setApp('Asaas');
103
104
        $this->manager->persist($order);
105
        $this->manager->flush();
106
        return $order;
107
    }
108
109
    public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
0 ignored issues
show
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

109
    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...
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

109
    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...
110
    {
111
        $companies   = $this->peopleService->getMyCompanies();
112
113
        if ($invoice = $this->request->query->get('invoiceId', null)) {
114
            $queryBuilder->join(sprintf('%s.invoice', $rootAlias), 'OrderInvoice');
115
            $queryBuilder->andWhere(sprintf('OrderInvoice.invoice IN(:invoice)', $rootAlias, $rootAlias));
116
            $queryBuilder->setParameter('invoice', $invoice);
117
        }
118
119
        $queryBuilder->andWhere(sprintf('%s.client IN(:companies) OR %s.provider IN(:companies)', $rootAlias, $rootAlias));
120
        $queryBuilder->setParameter('companies', $companies);
121
122
        if ($provider = $this->request->query->get('provider', null)) {
123
            $queryBuilder->andWhere(sprintf('%s.provider IN(:provider)', $rootAlias));
124
            $queryBuilder->setParameter('provider', preg_replace("/[^0-9]/", "", $provider));
125
        }
126
127
        if ($client = $this->request->query->get('client', null)) {
128
            $queryBuilder->andWhere(sprintf('%s.client IN(:client)', $rootAlias));
129
            $queryBuilder->setParameter('client', preg_replace("/[^0-9]/", "", $client));
130
        }
131
    }
132
}
133