DomainController::list()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Domain;
6
use App\Http\Requests\DomainAddRequest;
7
use App\Siweocs\Models\DomainAddResponse;
8
use App\Siweocs\Models\DomainListResponse;
9
use App\Siweocs\Models\SiwecosBaseReponse;
10
use App\Token;
11
use Illuminate\Database\QueryException;
12
use Illuminate\Http\Request;
13
use Keygen\Keygen;
14
15
class DomainController extends Controller
16
{
17
    /**
18
     * @param DomainAddRequest $request
19
     *
20
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
21
     */
22
    public function add(DomainAddRequest $request)
23
    {
24
        $domainFilter = parse_url($request->get('domain'));
25
        $domain = $domainFilter['scheme'].'://'.$domainFilter['host'];
26
27
        /** @var Domain $exisitingDomain */
28
        $exisitingDomain = Domain::whereDomain($domain)->first();
29
        if ($exisitingDomain instanceof Domain) {
0 ignored issues
show
introduced by
$exisitingDomain is always a sub-type of App\Domain. If $exisitingDomain can have other possible types, add them to app/Http/Controllers/DomainController.php:27.
Loading history...
30
            if ($exisitingDomain->verified === 1) {
0 ignored issues
show
introduced by
The condition $exisitingDomain->verified === 1 is always false.
Loading history...
31
                return response('Domain already there', 500);
32
            }
33
            /** @var Token $token */
34
            $token = Token::whereToken($request->header('siwecosToken'))->first();
35
            $exisitingDomain->token_id = $token->id;
36
            $exisitingDomain->domain_token = Keygen::alphanum(64)->generate();
37
38
            try {
39
                $exisitingDomain->save();
40
41
                return response()->json(new DomainAddResponse($exisitingDomain));
42
            } catch (QueryException $queryException) {
43
                return response($queryException->getMessage(), 500);
44
            }
45
        }
46
47
        $newDomain = new Domain([
48
            'domain' => $request->json('domain'),
49
            'token'  => $request->header('siwecosToken'),
50
        ]);
51
52
        try {
53
            $newDomain->save();
54
55
            return response()->json(new DomainAddResponse($newDomain));
56
        } catch (QueryException $queryException) {
57
            return response($queryException->getMessage(), 500);
58
        }
59
    }
60
61
    public function verify(Request $request)
62
    {
63
        $token = Token::getTokenByString($request->header('siwecosToken'));
64
        $domain = Domain::getDomainOrFail($request->json('domain'), $token->id);
0 ignored issues
show
Bug introduced by
It seems like $request->json('domain') can also be of type Symfony\Component\HttpFoundation\ParameterBag; however, parameter $domain of App\Domain::getDomainOrFail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $domain = Domain::getDomainOrFail(/** @scrutinizer ignore-type */ $request->json('domain'), $token->id);
Loading history...
65
        if (!$domain->checkHtmlPage()) {
66
            if (!$domain->checkMetatags()) {
67
                return response('Page not validate', 417);
68
            }
69
        }
70
71
        return response()->json(new SiwecosBaseReponse('Page successful validated'));
72
    }
73
74
    public function list(Request $request)
75
    {
76
        $token = Token::getTokenByString($request->header('siwecosToken'));
77
        $domains = $token->domains()->get();
78
79
        return response()->json(new DomainListResponse($domains));
80
    }
81
82
    public function remove(DomainAddRequest $request)
83
    {
84
        $domainFilter = parse_url($request->json('domain'));
0 ignored issues
show
Bug introduced by
It seems like $request->json('domain') can also be of type Symfony\Component\HttpFoundation\ParameterBag; however, parameter $url of parse_url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $domainFilter = parse_url(/** @scrutinizer ignore-type */ $request->json('domain'));
Loading history...
85
        $domain = $domainFilter['scheme'].'://'.$domainFilter['host'];
86
87
        $token = Token::getTokenByString($request->header('siwecosToken'));
88
        $domain = Domain::getDomainOrFail($domain, $token->id);
89
90
        try {
91
            $domain->delete();
92
        } catch (\Exception $e) {
93
            return response($e->getMessage(), 500);
94
        }
95
96
        return response()->json(new SiwecosBaseReponse('Domain removed'));
97
    }
98
}
99