Passed
Push — master ( c73812...aec782 )
by Luiz Kim
02:06
created

GetDefaultCompanyAction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 12

5 Methods

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