ProfileController::behaviors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 20
cp 0
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Controller;
13
14
use Da\User\Query\ProfileQuery;
15
use Yii;
16
use yii\base\Module;
17
use yii\filters\AccessControl;
18
use yii\web\Controller;
19
use yii\web\NotFoundHttpException;
20
21
class ProfileController extends Controller
22
{
23
    protected $profileQuery;
24
25
    /**
26
     * ProfileController constructor.
27
     *
28
     * @param string       $id
29
     * @param Module       $module
30
     * @param ProfileQuery $profileQuery
31
     * @param array        $config
32
     */
33
    public function __construct($id, Module $module, ProfileQuery $profileQuery, array $config = [])
34
    {
35
        $this->profileQuery = $profileQuery;
36
        parent::__construct($id, $module, $config);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function behaviors()
43
    {
44
        return [
45
            'access' => [
46
                'class' => AccessControl::class,
47
                'rules' => [
48
                    [
49
                        'allow' => true,
50
                        'actions' => ['index'],
51
                        'roles' => ['@'],
52
                    ],
53
                    [
54
                        'allow' => true,
55
                        'actions' => ['show'],
56
                        'roles' => ['?', '@'],
57
                    ],
58
                ],
59
            ],
60
        ];
61
    }
62
63
    public function actionIndex()
64
    {
65
        return $this->redirect(['show', 'id' => Yii::$app->user->getId()]);
66
    }
67
68
    public function actionShow($id)
69
    {
70
        $profile = $this->profileQuery->whereUserId($id)->one();
71
72
        if ($profile === null) {
73
            throw new NotFoundHttpException();
74
        }
75
76
        return $this->render(
77
            'show',
78
            [
79
                'profile' => $profile,
80
            ]
81
        );
82
    }
83
}
84