Passed
Push — master ( 9b4bfd...88cf06 )
by Luiz Kim
18:56 queued 16:44
created

GetDefaultCompanyAction::getDomain()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use Symfony\Component\Security\Core\Security;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Core\Security 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 Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse 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 Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request 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 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...
9
10
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...
11
use ControleOnline\Entity\People;
12
use ControleOnline\Entity\PeopleDomain;
13
use ControleOnline\Service\DomainService;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\DomainService 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...
14
use ControleOnline\Service\PeopleRoleService;
15
16
class GetDefaultCompanyAction
17
{
18
  /*
19
   * @var Security
20
   */
21
  private $security;
22
  private $em = null;
23
  private $roles;
24
  private $domain;
25
  private $company;
26
27
  public function __construct(
28
    Security $security,
29
    EntityManagerInterface $entityManager,
30
    PeopleRoleService $roles,
31
    private DomainService $domainService
32
  ) {
33
    $this->security = $security;
34
    $this->em = $entityManager;
35
    $this->roles = $roles;
36
  }
37
38
  public function __invoke(
39
    Request $request
40
  ): JsonResponse {
41
42
    try {
43
44
      $this->domain = $this->domainService->getDomain();
45
      $this->getCompany();
46
47
      $defaultCompany = [];
48
      $configs = [];
49
      $allConfigs = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $allConfigs is dead and can be removed.
Loading history...
50
      $user = $this->security->getUser();
51
52
      $permissions = $user ? $this->roles->getAllRoles($user->getPeople()) : ['guest'];
53
54
      if ($this->company) {
55
        $allConfigs = $this->em->getRepository(Config::class)->findBy([
56
          'people'      =>  $this->company->getPeople()->getId(),
57
          'visibility'  => 'public'
58
        ]);
59
60
        foreach ($allConfigs as $config) {
61
          $configs[$config->getConfigKey()] = $config->getConfigValue();
62
        }
63
64
        $defaultCompany = [
65
          'id'         => $this->company->getPeople()->getId(),
66
          'alias'      => $this->company->getPeople()->getAlias(),
67
          'configs'    => $configs,
68
          'domainType' => $this->company->getDomainType(),
69
          'permissions' => $permissions,
70
          'theme'       => $this->getTheme(),
71
          'logo'       => $this->company->getPeople()->getFile() ? [
72
            'id'     => $this->company->getPeople()->getFile()->getId(),
73
            'domain' => $this->domainService->getMainDomain(),
74
            'url'    => '/files/download/' . $this->company->getPeople()->getFile()->getId()
75
          ] : null,
76
        ];
77
      }
78
79
      return new JsonResponse([
80
        'response' => [
81
          'data'    => $defaultCompany,
82
          'count'   => 1,
83
          'error'   => '',
84
          'success' => true
85
        ],
86
      ]);
87
    } catch (\Exception $e) {
88
89
      return new JsonResponse([
90
        'response' => [
91
          'data'    => [],
92
          'count'   => 0,
93
          'error'   => $e->getMessage(),
94
          'success' => false,
95
        ],
96
      ]);
97
    }
98
  }
99
  private function getCompany()
100
  {
101
    $this->company = $this->em->getRepository(PeopleDomain::class)->findOneBy(['domain' => $this->domain]);
102
  }
103
104
  private function getTheme()
105
  {
106
    return [
107
      'theme' =>  $this->company->getTheme()->getTheme(),
108
      'colors' =>  $this->company->getTheme()->getColors(),
109
      'background'  =>  $this->company->getTheme()->getBackground() ? [
110
        'id'     =>  $this->company->getTheme()->getBackground(),
111
        'domain' => $this->domainService->getMainDomain(),
112
        'url'    => '/files/download/' .  $this->company->getTheme()->getBackground()
113
      ] : null,
114
    ];
115
  }
116
}
117