Passed
Push — master ( 4e05d4...d9ee28 )
by Luiz Kim
02:15
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
    private IntegrationService $integrationService
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\IntegrationService 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...
22
  ) {}
23
24
  public function addClientInteration(MessageInterface $message, People $provider, string $type): TaskInteration
25
  {
26
27
    $number = preg_replace('/\D/', '', $message->getOriginNumber());
28
    $name = '';
29
    $phone = [
30
      'ddi' => substr($number, 0, 2),
31
      'ddd' => substr($number, 2, 2),
32
      'phone' => substr($number, 4)
33
    ];
34
    $registredBy = $this->peopleService->discoveryPeople(null,  null,  $phone,  $name, null);
35
    $task = $this->discoveryOpenTask($provider, $registredBy, $type, $number);
36
37
    return $this->addInteration($registredBy, $message, $task, $type, 'public');
38
  }
39
40
  public function addInteration(People $registredBy, MessageInterface $message, Task $task, string $type, ?string $visibility = 'private')
41
  {
42
43
    $file = null;
44
    $media = $message->getMessageContent()->getMedia();
45
    if ($media)
46
      $file = $this->fileService->addFile(
47
        $registredBy,
48
        pack("C*", ...$media->getData()),
49
        $type
50
      );
51
52
    $taskInteration = new TaskInteration();
53
    $taskInteration->setTask($task);
54
    $taskInteration->setType($type);
55
    $taskInteration->setVisibility($visibility);
56
    $taskInteration->setBody($message->getMessageContent()->getBody());
57
    $taskInteration->setRegisteredBy($registredBy);
58
    if ($file)
59
      $taskInteration->setFile($file);
60
61
    $this->manager->persist($taskInteration);
62
    $this->manager->flush();
63
64
    return $taskInteration;
65
  }
66
67
  public function discoveryOpenTask(People $provider, People $registredBy, string $type, ?string $announce = null): Task
68
  {
69
70
    $openStatus = $this->statusService->discoveryStatus('open', 'open', $type);
71
    $pendingStatus = $this->statusService->discoveryStatus('pending', 'pending', $type);
72
73
    $task = $this->manager->getRepository(Task::class)->findOneBy([
74
      'taskStatus' => [$openStatus, $pendingStatus],
75
      'provider' => $provider,
76
      'registeredBy' => $registredBy,
77
      'type' => $type
78
    ]);
79
80
    if (!$task) {
81
      $task = new Task();
82
      $task->setRegisteredBy($registredBy);
83
      $task->setProvider($provider);
84
      $task->settype($type);
85
    }
86
87
    if ($announce) $task->addAnnounce($announce);
88
    $task->setTaskStatus($openStatus);
89
    $this->manager->persist($task);
90
    $this->manager->flush();
91
92
    return $task;
93
  }
94
95
  public function notifyClient(TaskInteration $taskInteration): TaskInteration
96
  {
97
98
    $task = $taskInteration->getTask();
99
    $origin = "551131360353";
100
101
    foreach ($task->getAnnounce(true) as $destination) {
102
      if ($origin != $destination) {
103
        $message = json_encode([
104
          "action" => "sendMessage",
105
          "origin" => $origin,
106
          "destination" => $destination,
107
          "message" => json_encode($taskInteration->getBody()),
108
          //"file" => $taskInteration->getFile()
109
        ]);
110
        $this->integrationService->addIntegration($message, 'WhatsApp', null, null, $task->getProvider());
111
      }
112
    }
113
114
    return  $taskInteration;
115
  }
116
117
  public function prePersist(TaskInteration $taskInteration): TaskInteration
118
  {
119
    if (!$taskInteration->getRegisteredBy())
120
      $taskInteration->setRegisteredBy($this->security->getToken()->getUser()->getPeople());
121
    return  $taskInteration;
122
  }
123
124
  public function postPersist(TaskInteration $taskInteration): TaskInteration
125
  {
126
    return $this->notifyClient($taskInteration);
127
  }
128
}
129