Passed
Push — master ( 88cf06...50e7c4 )
by Luiz Kim
02:16
created

PeopleService::beforePersist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 5
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace ControleOnline\Service;
4
5
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...
6
use ControleOnline\Entity\People;
7
use ControleOnline\Entity\PeopleLink;
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\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...
10
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...
11
12
class PeopleService
13
{
14
15
  public function __construct(
16
    private EntityManagerInterface $manager,
17
    private Security               $security,
18
    private RequestStack $requestStack
19
  ) {
20
  }
21
22
  public function beforePersist(People $people)
23
  {
24
    $language = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']);
25
    $people->setLanguage($language);
26
    return $people;
27
  }
28
29
  public function afterPersist(People $people)
30
  {
31
    $request = $this->requestStack->getCurrentRequest();
32
    $payload   = json_decode($request->getContent(), true);
33
    if (isset($payload->link_type)) {
34
      $company = $this->manager->getRepository(PeopleLink::class)->find($payload->company);
35
      $this->addLink($company, $people, $payload->link_type);
36
    }
37
  }
38
39
  public function addLink(People $company, People $people, $link_type)
40
  {
41
    
42
    $peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([
43
      'company' => $company,
44
      'people' => $people,
45
      'link_type' => $link_type
46
    ]);
47
48
    if (!$peopleLink)
49
      $peopleLink = new PeopleLink();
50
51
    $peopleLink->setCompany($company);
52
    $peopleLink->setPeople($people);
53
    $peopleLink->setLinkType($link_type);
54
55
    $this->manager->persist($peopleLink);
56
    $this->manager->flush();
57
58
    return  $peopleLink;
59
  }
60
}
61