CreatePermissionTask::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\Authorization\Tasks;
4
5
use App\Containers\Authorization\Data\Repositories\PermissionRepository;
6
use App\Containers\Authorization\Models\Permission;
7
use App\Ship\Exceptions\CreateResourceFailedException;
8
use App\Ship\Parents\Tasks\Task;
9
use Exception;
10
11
/**
12
 * Class CreatePermissionTask
13
 *
14
 * @author  Mahmoud Zalt  <[email protected]>
15
 */
16 View Code Duplication
class CreatePermissionTask extends Task
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
19
    /**
20
     * @var  \App\Containers\Authorization\Data\Repositories\PermissionRepository
21
     */
22
    protected $repository;
23
24
    /**
25
     * CreatePermissionTask constructor.
26
     *
27
     * @param \App\Containers\Authorization\Data\Repositories\PermissionRepository $repository
28
     */
29
    public function __construct(PermissionRepository $repository)
30
    {
31
        $this->repository = $repository;
32
    }
33
34
    /**
35
     * @param string      $name
36
     * @param string|null $description
37
     * @param string|null $displayName
38
     *
39
     * @return Permission
40
     * @throws CreateResourceFailedException
41
     */
42
    public function run(string $name, string $description = null, string $displayName = null): Permission
43
    {
44
        app()['cache']->forget('spatie.permission.cache');
45
46
        try {
47
            $permission = $this->repository->create([
48
                'name'         => $name,
49
                'description'  => $description,
50
                'display_name' => $displayName,
51
                'guard_name'   => 'web',
52
            ]);
53
        } catch (Exception $exception) {
54
            throw new CreateResourceFailedException();
55
        }
56
57
        return $permission;
58
    }
59
}
60