Passed
Push — master ( 67f36e...133ff2 )
by Luiz Kim
06:03 queued 04:03
created

SignatureService::sign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ControleOnline\Service;
4
5
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...
6
use App\Library\Provider\Signature\SignatureFactory;
0 ignored issues
show
Bug introduced by
The type App\Library\Provider\Signature\SignatureFactory 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...
7
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...
8
use ControleOnline\Entity\Config;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Config 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 ControleOnline\Entity\Contract;
10
use ControleOnline\Entity\File;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\File 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 SignatureService
13
{
14
15
  public function __construct(
16
    private EntityManagerInterface $manager,
17
    private PeopleRoleService $peopleRoleService,
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PeopleRoleService 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...
18
    private PdfService $pdf,
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PdfService 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 ModelService $modelService
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\ModelService 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
  ) {}
21
22
  public function genetateFromModel(Contract $data)
23
  {
24
    $file = $data->getContractFile();
25
    if (!$file)
26
      $file = new File();
27
28
    $file->setFileType('text');
29
    $file->setExtension('html');
30
    $file->setContent($this->modelService->genetateFromModel($data));
31
    $this->manager->persist($file);
32
    $this->manager->flush();
33
34
    return $data;
35
  }
36
37
38
  public function getContractPDFContent(Contract $contract): string
39
  {
40
    $content = $contract->getContractFile()->getContent();
41
42
    if ($contract->getContractFile()->getExtension() == 'pdf')
43
      return $content;
44
45
    if (empty($content)) {
46
      throw new \Exception(
47
        sprintf('Houve um erro ao gerar o PDF')
48
      );
49
    }
50
51
    return $this->pdf->convertHtmlToPdf($content);
52
  }
53
54
  public function getFactory(?string $factoryName = null): ?SignatureFactory
55
  {
56
    $providerName = $factoryName === null ?
57
      $this->getDefaultProviderFromConfig() : $factoryName;
58
59
    if ($providerName === null) {
60
      return null;
61
    }
62
63
    $provider = sprintf(
64
      '\\App\\Library\\Provider\\Signature\\%s\\Factory',
65
      ucfirst(strtolower($providerName))
66
    );
67
68
    if (!class_exists($provider)) {
69
      throw new \Exception('Signature provider factory not found');
70
    }
71
72
    return new $provider(
73
      $this->getProviderConfig($providerName)
74
    );
75
  }
76
77
78
  private function getProviderConfig(string $providerName): ?array
79
  {
80
81
    $myCompany = $this->peopleRoleService->getMainCompany();
82
    if ($myCompany instanceof People) {
83
84
      return $this->manager->getRepository(Config::class)
85
        ->getKeyValuesByPeople(
86
          $myCompany,
87
          strtolower($providerName)
88
        );
89
    }
90
91
    throw new \Exception('Company not found');
92
  }
93
94
  private function getDefaultProviderFromConfig(): ?string
95
  {
96
    $myCompany = $this->peopleRoleService->getMainCompany();
97
    if ($myCompany instanceof People) {
98
      $configs = $this->manager->getRepository(Config::class)
99
        ->getKeyValuesByPeople(
100
          $myCompany,
101
          'provider'
102
        );
103
104
      if ($configs === null) {
105
        return null;
106
      }
107
108
      return isset($configs['provider-signature']) ?
109
        $configs['provider-signature'] : null;
110
    }
111
112
    throw new \Exception('Company not found');
113
  }
114
}
115