Passed
Push — master ( 84cccb...8cc76a )
by Luiz Kim
12:27 queued 04:10
created

TaskInterationService::addInteration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 25
rs 9.6666
cc 3
nc 4
nop 5
1
<?php
2
3
namespace ControleOnline\Service;
4
5
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...
6
use ControleOnline\Entity\Task;
7
use ControleOnline\Entity\TaskInteration;
8
use ControleOnline\Messages\MessageInterface;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Messages\MessageInterface 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 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...
10
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface as Security;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface 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 TaskInterationService
13
{
14
15
  public function __construct(
16
    private EntityManagerInterface $manager,
17
    private Security $security,
18
    private StatusService $statusService,
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\StatusService 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...
19
    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...
20
    private FileService $fileService
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\FileService 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
  ) {}
22
23
  public function addClientInteration(MessageInterface $message, People $provider, string $type): TaskInteration
24
  {
25
26
    $number = preg_replace('/\D/', '', $message->getOriginNumber());
27
    $name = '';
28
    $phone = [
29
      'ddi' => substr($number, 0, 2),
30
      'ddd' => substr($number, 2, 2),
31
      'phone' => substr($number, 4)
32
    ];
33
    $registredBy = $this->peopleService->discoveryPeople(null,  null,  $phone,  $name, null);
34
    $task = $this->discoveryOpenTask($provider, $registredBy, $type);
35
    return $this->addInteration($registredBy, $message, $task, $type, 'public');
36
  }
37
38
  public function addInteration(People $registredBy, MessageInterface $message, Task $task, string $type, ?string $visibility = 'private')
39
  {
40
41
    $file = null;
42
    $media = $message->getMessageContent()->getMedia();
43
    if ($media)
44
      $file = $this->fileService->addFile(
45
        $registredBy,
46
        pack("C*", ...$media->getData()),
47
        $type
48
      );
49
50
    $taskInteration = new TaskInteration();
51
    $taskInteration->setTask($task);
52
    $taskInteration->settype($type);
53
    $taskInteration->setVisibility($visibility);
54
    $taskInteration->setBody($message->getMessageContent()->getBody());
55
    $taskInteration->setRegisteredBy($registredBy);
56
    if ($file)
57
      $taskInteration->setFile($file);
58
59
    $this->manager->persist($taskInteration);
60
    $this->manager->flush();
61
62
    return $taskInteration;
63
  }
64
65
  public function discoveryOpenTask(People $provider, People $registredBy, string $type): Task
66
  {
67
68
    $openStatus = $this->statusService->discoveryStatus('open', 'open', $type);
69
    $pendingStatus = $this->statusService->discoveryStatus('pending', 'pending', $type);
70
71
    $task = $this->manager->getRepository(Task::class)->findOneBy([
72
      'status' => [$openStatus, $pendingStatus],
73
      'provider' => $provider,
74
      'registredBy' => $registredBy,
75
      'type' => $type
76
    ]);
77
78
    if (!$task) {
79
      $task = new Task();
80
      $task->setRegisteredBy($registredBy);
81
      $task->setProvider($provider);
82
      $task->settype($type);
83
    }
84
85
    $task->setTaskStatus($openStatus);
86
    $this->manager->persist($task);
87
    $this->manager->flush();
88
89
    return $task;
90
  }
91
92
  public function prePersist(TaskInteration $taskInteration): TaskInteration
93
  {
94
    if (!$taskInteration->getRegisteredBy())
95
      $taskInteration->setRegisteredBy($this->security->getToken()->getUser()->getPeople());
96
    return  $taskInteration;
97
  }
98
}
99