Passed
Push — master ( 48efbe...67f36e )
by Luiz Kim
02:44
created

SignatureService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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