Completed
Pull Request — master (#163)
by Corey
03:24
created

CheckinController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 119
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 11 1
B actionView() 0 30 3
A actionIndex() 0 21 4
B actionQuestions() 0 32 6
A actionReport() 0 8 1
1
<?php
2
3
namespace site\controllers;
4
5
use Yii;
6
use common\models\Category;
7
use common\models\Behavior;
8
use common\interfaces\UserInterface;
9
use common\interfaces\UserBehaviorInterface;
10
use common\Interfaces\TimeInterface;
11
use yii\di\Container;
12
use yii\base\InvalidParamException;
13
use yii\web\BadRequestHttpException;
14
use yii\web\Controller;
15
use yii\filters\VerbFilter;
16
use common\components\AccessControl;
17
use yii\helpers\ArrayHelper as AH;
18
19
class CheckinController extends \yii\web\Controller
20
{
21
  public function behaviors()
22
  {
23
    return [
24
      'access' => [
25
        'class' => AccessControl::class,
26
        'only' => ['index', 'view', 'questions', 'report'],
27
        'rules' => [
28
          [
29
            'actions' => ['index', 'view', 'questions', 'report'],
30
            'allow' => true,
31
            'roles' => ['@'],
32
          ],
33
        ],
34
      ],
35
    ];
36
  }
37
  public function actionIndex() {
38
    $form = Yii::$container->get(\site\models\CheckinForm::class);
39
    if ($form->load(Yii::$app->request->post()) && $form->validate()) {
40
      $form->compiled_behaviors = $form->compileBehaviors();
41
42
      if(sizeof($form->compiled_behaviors) === 0) {
43
        return $this->redirect(['view']);
44
      }
45
46
      // we only store one data set per day, so wipe out previously saved ones
47
      $form->deleteToday();
48
      $form->save();
49
50
      return $this->redirect(['questions']);
51
    } else {
52
      $category = Yii::$container->get(\common\interfaces\CategoryInterface::class);
53
      $behavior = Yii::$container->get(\common\interfaces\BehaviorInterface::class);
54
      return $this->render('index', [
55
        'categories'    => $category::$categories,
56
        'model'         => $form,
57
        'behaviorsList' => AH::index($behavior::$behaviors, null, "category_id")
58
      ]);
59
    }
60
  }
61
62
  public function actionQuestions()
63
  {
64
    $user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class);
65
    $date = Yii::$container->get(\common\interfaces\TimeInterface::class)->getLocalDate();
66
67
    $user_behaviors = $user_behavior->getUserBehaviorsWithCategory($date);
68
    if(count($user_behaviors) === 0) {
69
      return $this->redirect(['view']);
70
    }
71
72
    $form = Yii::$container->get(\site\models\QuestionForm::class);
73
    if ($form->load(Yii::$app->request->post()) && $form->validate()) {
74
      // we only store one data set per day so clear out any previously saved ones
75
      $form->deleteToday();
76
77
      $behaviors = $user_behavior->findAll($form->getUserBehaviorIds());
78
      if($result = $form->saveAnswers($behaviors)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
79
80
        if(Yii::$app->user->identity->send_email) {
0 ignored issues
show
Bug introduced by
Accessing send_email on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
81
          Yii::$app->user->identity->sendEmailReport($date);
0 ignored issues
show
Bug introduced by
The method sendEmailReport() does not exist on null. ( Ignorable by Annotation )

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

81
          Yii::$app->user->identity->/** @scrutinizer ignore-call */ 
82
                                     sendEmailReport($date);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method sendEmailReport() does not exist on yii\web\IdentityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in site\tests\_support\MockUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

81
          Yii::$app->user->identity->/** @scrutinizer ignore-call */ 
82
                                     sendEmailReport($date);
Loading history...
82
          Yii::$app->session->setFlash('success', 'Your check-in is complete. A notification has been sent to your report partners.');
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

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

82
          Yii::$app->session->/** @scrutinizer ignore-call */ 
83
                              setFlash('success', 'Your check-in is complete. A notification has been sent to your report partners.');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
        } else {
84
          Yii::$app->session->setFlash('success', 'Your check-in is complete.');
85
        }
86
87
        return $this->redirect(['view']);
88
      }
89
    }
90
91
    return $this->render('questions', [
92
      'model' => $form,
93
      'behaviors' => $user_behaviors
94
    ]);	
95
  }
96
97
  public function actionView(string $date = null)
98
  {
99
    $time = Yii::$container->get(\common\interfaces\TimeInterface::class);
100
    if(is_null($date)) {
101
      $date = $time->getLocalDate();
102
    } else if($dt = $time->parse($date)) {
103
      $date = $dt->format('Y-m-d');
104
    } else {
105
      $date = $time->getLocalDate();
106
    }
107
    $dt = $time->parse($date);
108
    list($start, $end) = $time->getUTCBookends($date);
0 ignored issues
show
Comprehensibility Best Practice introduced by
This list assign is not used and could be removed.
Loading history...
109
110
    $user          = Yii::$container->get(\common\interfaces\UserInterface::class);
111
    $user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class);
112
    $category      = Yii::$container->get(\common\interfaces\CategoryInterface::class);
113
    $behavior      = Yii::$container->get(\common\interfaces\BehaviorInterface::class);
114
115
    $form = Yii::$container->get(\site\models\CheckinForm::class);
116
    $form->setBehaviors($user->getUserBehaviors($date));
117
118
    return $this->render('view', [
119
      'model'              => $form,
120
      'actual_date'        => $date,
121
      'categories'         => $category::$categories,
122
      'behaviorsList'      => AH::index($behavior::$behaviors, 'name', "category_id"),
123
      'past_checkin_dates' => $user_behavior->getPastCheckinDates(),
124
      'answer_pie'         => $user_behavior->getBehaviorsByCategory($dt),
125
      'questions'          => $user->getUserQuestions($date),
126
      'isToday'            => $time->getLocalDate() === $date,
127
    ]);
128
  }
129
130
  public function actionReport() {
131
    $user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class);
132
    $user_rows  = $user_behavior->getTopBehaviors();
133
    $answer_pie = $user_behavior->getBehaviorsByCategory();
134
135
    return $this->render('report', [
136
      'top_behaviors' => $user_rows,
137
      'answer_pie' => $answer_pie,
138
    ]);
139
  }
140
}
141