TenantService::createOrUpdate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AtlassianConnectCore\Services;
4
5
use AtlassianConnectCore\Models\Tenant;
6
use AtlassianConnectCore\Repositories\TenantRepository;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
/**
10
 * Class TenantService
11
 *
12
 * @package AtlassianConnectCore\Services
13
 */
14
class TenantService
15
{
16
    /**
17
     * @var TenantRepository
18
     */
19
    protected $repository;
20
21
    /**
22
     * TenantService constructor.
23
     *
24
     * @param TenantRepository $tenantRepository
25
     */
26
    public function __construct(TenantRepository $tenantRepository)
27
    {
28
        $this->repository = $tenantRepository;
29
    }
30
31
    /**
32
     * Create or update a tenant
33
     *
34
     * @param bool $withTrashed
35
     *
36
     * @return \Illuminate\Database\Eloquent\Collection|Tenant[]
37
     */
38
    public function all($withTrashed = false)
39
    {
40
        return $this->repository->findAll($withTrashed);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->repository->findAll($withTrashed) returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type AtlassianConnectCore\Mod...ase\Eloquent\Collection.
Loading history...
41
    }
42
43
    /**
44
     * Create or update a tenant
45
     *
46
     * @param array $attributes
47
     *
48
     * @return Tenant
49
     */
50
    public function createOrUpdate(array $attributes)
51
    {
52
        /** @var Tenant|null $model */
53
        $model = $this->repository
54
            ->findWhere(['client_key' => $attributes['client_key']])
55
            ->first();
56
57
        if(!$model) {
58
            return $this->repository->create($attributes);
59
        }
60
61
        return $this->repository->update($model->id, $attributes);
62
    }
63
64
    /**
65
     * Update state
66
     *
67
     * @param string $clientKey
68
     * @param string $eventType
69
     *
70
     * @return Tenant
71
     */
72
    public function updateState($clientKey, $eventType)
73
    {
74
        return $this->repository->updateWhere(['client_key' => $clientKey], ['event_type' => $eventType]);
75
    }
76
77
    /**
78
     * Find a tenant by client key
79
     *
80
     * @param string $clientKey
81
     * @param bool $withTrashed
82
     *
83
     * @return Tenant|null
84
     */
85
    public function findByClientKey($clientKey, $withTrashed = true)
86
    {
87
        return $this->repository
88
            ->findWhere(['client_key' => $clientKey], $withTrashed)
89
            ->first();
90
    }
91
92
    /**
93
     * Find a tenant by client key
94
     *
95
     * @param string $clientKey
96
     * @param bool $withTrashed
97
     *
98
     * @return Tenant
99
     *
100
     * @throws NotFoundHttpException
101
     */
102
    public function findByClientKeyOrFail($clientKey, $withTrashed = true)
103
    {
104
        if($tenant = $this->findByClientKey($clientKey, $withTrashed)) {
105
            return $tenant;
106
        }
107
108
        throw new NotFoundHttpException();
109
    }
110
111
    /**
112
     * Find not-dummied tenants
113
     *
114
     * @return \Illuminate\Database\Eloquent\Collection|Tenant[]
115
     */
116
    public function findReals()
117
    {
118
        return $this->repository->findWhere(['is_dummy' => false], false);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->repository...ummy' => false), false) returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type AtlassianConnectCore\Mod...ase\Eloquent\Collection.
Loading history...
119
    }
120
121
    /**
122
     * Delete the tenant
123
     *
124
     * @param int $id
125
     *
126
     * @return mixed
127
     */
128
    public function delete($id)
129
    {
130
        if(config('plugin.safeDelete')) {
131
            return $this->repository->delete($id);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->repository->delete($id) targeting AtlassianConnectCore\Rep...antRepository::delete() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
132
        }
133
134
        return $this->repository->forceDelete($id);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->repository->forceDelete($id) targeting AtlassianConnectCore\Rep...pository::forceDelete() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
135
    }
136
137
    /**
138
     * Get dummy tenant
139
     *
140
     * @return Tenant|null
141
     */
142
    public function dummy()
143
    {
144
        return $this->repository->findDummy();
145
    }
146
147
    /**
148
     * Make tenant dummied
149
     *
150
     * @param int $id Tenant ID
151
     *
152
     * @return Tenant|null
153
     */
154
    public function makeDummy($id)
155
    {
156
        if(!$this->repository->findById($id)) {
157
            throw new NotFoundHttpException();
158
        }
159
160
        return $this->repository->updateWhere(['id' => $id], ['is_dummy' => true]);
161
    }
162
}