HttpBasicUserProvider::getUser()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 10
cp 0.8
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 4.128
1
<?php
2
3
/*
4
 * This file is part of the "Kata 1" package.
5
 *
6
 * Copyright (c) Daniel González
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Daniel González <[email protected]>
12
 */
13
14
namespace App\Security\Http;
15
16
use Component\Http\Request;
17
18
use App\Repository\UserRepository;
19
20
/**
21
 * HttpBasicUserProvider.
22
 */
23
class HttpBasicUserProvider
24
{
25
    /**
26
     * @var UserRepository
27
     */
28
    protected $repository;
29
30
    /**
31
     * HttpBasicUserProvider constructor.
32
     *
33
     * @param UserRepository $repository
34
     */
35 9
    public function __construct(UserRepository $repository)
36
    {
37 9
        $this->repository = $repository;
38 9
    }
39
40
    /**
41
     * @param Request|null $request
42
     *
43
     * @return \App\Model\User|false
44
     */
45 9
    public function getUser(Request $request = null)
46
    {
47 9
        if (!$request) {
48
            return;
49
        }
50 9
        $token = base64_decode($request->getHeader('Authorization'));
51 9
        if (!$token) {
52 4
            return;
53
        }
54 5
        if (!strpos($token, ':')) {
55
            return;
56
        }
57 5
        list($name, $password) = explode(':', $token);
58
59 5
        return $this->repository->findByNameAndPassword($name, $password);
60
    }
61
}
62