Completed
Pull Request — master (#164)
by Corey
03:20
created

ProfileController::actionExport()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
namespace site\controllers;
3
4
5
use Yii;
6
use common\models\Question;
7
use yii\web\Controller;
8
use yii\filters\VerbFilter;
9
use common\components\AccessControl;
10
use League\Csv\Writer;
0 ignored issues
show
Bug introduced by
The type League\Csv\Writer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Profile controller
14
 */
15
class ProfileController extends Controller {
16
  /**
17
   * @inheritdoc
18
   */
19
  public function behaviors() {
20
    return [
21
      'access' => [
22
        'class' => AccessControl::class,
23
        'rules' => [
24
          [
25
            'actions' => ['index', 'error', 'delete-account', 'change-password', 'change-email', 'export'],
26
            'allow' => true,
27
            'roles' => ['@'],
28
          ],
29
        ],
30
      ],
31
      'verbs' => [
32
        'class' => VerbFilter::class,
33
        'actions' => [
34
          'deleteAccount' => ['post'],
35
          'changePassword' => ['post'],
36
        ],
37
      ],
38
    ];
39
  }
40
41
  /**
42
   * @inheritdoc
43
   */
44
  public function actions()
45
  {
46
    return [
47
      'error' => [
48
        'class' => 'yii\web\ErrorAction',
49
      ],
50
      'captcha' => [
51
        'class' => 'yii\captcha\CaptchaAction',
52
      ],
53
    ];
54
  }
55
56
  public function actionIndex() {
57
    $editProfileForm    = Yii::$container->get(\site\models\EditProfileForm::class, [Yii::$app->user->identity]);
58
    $changePasswordForm = Yii::$container->get(\site\models\ChangePasswordForm::class, [Yii::$app->user->identity]);
59
    $changeEmailForm = Yii::$container->get(\site\models\ChangeEmailForm::class, [Yii::$app->user->identity]);
60
    $deleteAccountForm  = Yii::$container->get(\site\models\DeleteAccountForm::class, [Yii::$app->user->identity]);
61
    $graph              = Yii::$container->get(\common\components\Graph::class, [Yii::$app->user->identity]);
62
63
    if (Yii::$app->request->isAjax && $editProfileForm->load($_POST)) {
64
      Yii::$app->response->format = 'json';
65
      return \yii\widgets\ActiveForm::validate($editProfileForm);
66
    }
67
    $editProfileForm->loadUser();
68
69
    if ($editProfileForm->load(Yii::$app->request->post())) {
70
      $saved_user = $editProfileForm->saveProfile();
71
      if($saved_user) {
72
        Yii::$app->getSession()->setFlash('success', 'New profile data saved!');
73
      }
74
    }
75
76
    return $this->render('index', [
77
      'profile'         => $editProfileForm,
78
      'change_password' => $changePasswordForm,
79
      'change_email'    => $changeEmailForm,
80
      'delete'          => $deleteAccountForm,
81
      'graph_url'       => $graph->getUrl(Yii::$app->user->identity->getIdHash()),
0 ignored issues
show
Bug introduced by
The method getIdHash() does not exist on yii\web\IdentityInterface. Did you maybe mean getId()? ( Ignorable by Annotation )

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

81
      'graph_url'       => $graph->getUrl(Yii::$app->user->identity->/** @scrutinizer ignore-call */ getIdHash()),

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 getIdHash() 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
      'graph_url'       => $graph->getUrl(Yii::$app->user->identity->/** @scrutinizer ignore-call */ getIdHash()),

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...
82
    ]);
83
  }
84
85
  public function actionDeleteAccount() {
86
    $model = Yii::$container->get(\site\models\DeleteAccountForm::class, [Yii::$app->user->identity]);
87
88
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
89
      if($model->deleteAccount()) {
90
        $this->redirect(['site/index']);
91
      } else {
92
        Yii::$app->getSession()->setFlash('error', 'Wrong password!');
93
      }
94
    }
95
96
    $this->redirect(Yii::$app->request->getReferrer());
97
  }
98
99
  public function actionChangePassword() {
100
    $model = Yii::$container->get(\site\models\ChangePasswordForm::class, [Yii::$app->user->identity]);
101
102
    if ($model->load(Yii::$app->request->post())) {
103
      if($model->validate() && $model->changePassword()) {
104
        Yii::$app->getSession()->setFlash('success', 'Password successfully changed');
105
      } else {
106
        Yii::$app->getSession()->setFlash('error', 'Wrong password!');
107
      }
108
    }
109
110
    $this->redirect(['profile/index']);
111
  }
112
113
  public function actionChangeEmail() {
114
    $model = Yii::$container->get(\site\models\ChangeEmailForm::class, [Yii::$app->user->identity]);
115
116
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
117
      if($model->changeEmail()) {
118
        Yii::$app->getSession()->setFlash('success', "We've sent an email to your requested email address to confirm. Please click on the verification link.");
119
      } else {
120
        Yii::$app->getSession()->setFlash('error', 'Something went wrong!');
121
      }
122
    }
123
124
    $this->redirect(['profile/index']);
125
  }
126
127
  public function actionExport() {
128
    header("Content-Type: text/csv");
129
    header("Content-Disposition: attachment; filename=fsa-data-export-".Yii::$app->user->identity->email."-".date('Ymd').".csv");
0 ignored issues
show
Bug introduced by
Accessing email on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
130
131
    $reader = Yii::$app->user->identity->getExportData();
0 ignored issues
show
Bug introduced by
The method getExportData() 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

131
    /** @scrutinizer ignore-call */ 
132
    $reader = Yii::$app->user->identity->getExportData();
Loading history...
132
    $fp = fopen('php://output', 'w');
133
134
    $header = [
135
      'Date',
136
      'Behavior',
137
      'Category',
138
      Question::$QUESTIONS[1],
139
      Question::$QUESTIONS[2],
140
      Question::$QUESTIONS[3],
141
    ];
142
143
    fputcsv($fp, $header);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, 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

143
    fputcsv(/** @scrutinizer ignore-type */ $fp, $header);
Loading history...
144
    $user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class);
145
    while($row = $reader->read()) {
146
      $row = $user_behavior::decorateWithCategory([$row]);
147
      $row = Yii::$app->user->identity->cleanExportData($row);
0 ignored issues
show
Bug introduced by
The method cleanExportData() 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

147
      /** @scrutinizer ignore-call */ 
148
      $row = Yii::$app->user->identity->cleanExportData($row);
Loading history...
148
      fputcsv($fp, $row[0]);
149
    }
150
    fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

150
    fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
151
152
    die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
153
  }
154
}
155