Passed
Push — master ( b633b9...638f09 )
by Luiz Kim
12:22 queued 09:56
created

PeopleService::getMyCompanies()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 10
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Document;
6
use ControleOnline\Entity\DocumentType;
7
use ControleOnline\Entity\Language;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Language 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
use ControleOnline\Entity\People;
9
use ControleOnline\Entity\PeopleLink;
10
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...
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
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...
13
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...
14
15
class PeopleService
16
{
17
  private $request;
18
19
  public function __construct(
20
    private EntityManagerInterface $manager,
21
    private Security               $security,
22
    private RequestStack $requestStack,
23
    private PeopleService $PeopleService
24
  ) {
25
    $this->request  = $requestStack->getCurrentRequest();
26
  }
27
28
  public function beforePersist(People $people)
29
  {
30
    $language = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']);
31
    $people->setLanguage($language);
32
    return $people;
33
  }
34
35
  public function afterPersist(People $people)
36
  {
37
    $request = $this->requestStack->getCurrentRequest();
38
    $payload   = json_decode($request->getContent());
39
    if (isset($payload->link_type)) {
40
      $company = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->company));
41
      if ($company)
42
        $this->addLink($company, $people, $payload->link_type);
43
      else {
44
        $link = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->link));
45
        if ($payload->link_type == 'employee' && $link) {
46
          $this->addLink($people, $link, $payload->link_type);
47
          if ($payload->people_document) {
48
            $document_type = $this->manager->getRepository(DocumentType::class)->findOneBy(['document_type' => 'cnpj']);
49
50
            $document = new Document();
51
            $document->setPeople($people);
52
            $document->setDocumentType($document_type);
53
            $document->setDocument($payload->people_document);
54
            $this->manager->persist($document);
55
            $this->manager->flush();
56
          }
57
        }
58
      }
59
    }
60
  }
61
62
  public function addLink(People $company, People $people, $link_type)
63
  {
64
65
    $peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([
66
      'company' => $company,
67
      'people' => $people,
68
      'link_type' => $link_type
69
    ]);
70
71
    if (!$peopleLink)
72
      $peopleLink = new PeopleLink();
73
74
    $peopleLink->setCompany($company);
75
    $peopleLink->setPeople($people);
76
    $peopleLink->setLinkType($link_type);
77
78
    $this->manager->persist($peopleLink);
79
    $this->manager->flush();
80
    return  $peopleLink;
81
  }
82
83
  public function secutiryFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
84
  {
85
    $this->checkLink($queryBuilder, $resourceClass, $applyTo, $rootAlias);
86
  }
87
88
  public function checkLink(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
0 ignored issues
show
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

88
  public function checkLink(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

88
  public function checkLink(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...
89
  {
90
91
    $link   = $this->request->query->get('link',   null);
92
    $company = $this->request->query->get('company', null);
93
    $link_type = $this->request->query->get('link_type', null);
94
95
    if ($link_type) {
96
      $queryBuilder->join(sprintf('%s.' . ($link ? 'company' : 'link'), $rootAlias), 'PeopleLink');
97
      $queryBuilder->andWhere('PeopleLink.link_type IN(:link_type)');
98
      $queryBuilder->setParameter('link_type', $link_type);
99
    }
100
101
    if ($company || $link) {
102
      $queryBuilder->andWhere('PeopleLink.' . ($link ? 'people' : 'company') . ' IN(:people)');
103
      $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", ($link ?: $company)));
104
    }
105
  }
106
  public function checkCompany($type, 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

106
  public function checkCompany($type, 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

106
  public function checkCompany($type, 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...
107
  {
108
    $companies   = $this->getMyCompanies();
109
    $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:companies)', $rootAlias, $rootAlias));
110
    $queryBuilder->setParameter('companies', $companies);
111
112
    if ($payer = $this->request->query->get('company', null)) {
113
      $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:people)', $rootAlias));
114
      $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", $payer));
115
    }
116
  }
117
118
119
120
  public function getMyCompanies(): array
121
  {
122
    /**
123
     * @var \ControleOnline\Entity\User $currentUser
124
     */
125
    $currentUser  = $this->security->getUser();
126
    $companies    = [];
127
128
    if (!$currentUser->getPeople()->getLink()->isEmpty()) {
129
      foreach ($currentUser->getPeople()->getLink() as $company) {
130
        $companies[] = $company->getCompany();
131
      }
132
    }
133
    return $companies;
134
  }
135
}
136