TaskInterationService::addInteration()   A
last analyzed

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