<?php
/**
 * Created by IntelliJ IDEA.
 * User: rocho
 * Date: 2018.08.29.
 * Time: 21:58
 */

namespace customerapi\controllers;

use common\models\Customer;
use customerapi\models\PasswordChangeForm;
use yii\web\BadRequestHttpException;

/** @noinspection PhpUnused */

class UserController extends RestController
{

    /**
     * @throws \yii\base\InvalidConfigException
     * @throws \yii\base\Exception
     * @throws BadRequestHttpException
     */
    public function actionPasswordChange()
    {
        $form = new PasswordChangeForm();
        $form->scenario = "default";

        $form->load(\Yii::$app->request->post(), '');

        if (!$form->validate()) {
            throw new BadRequestHttpException(  $form->getErrorSummary(false)[0]);
        }

        $customer = Customer::findOne(\Yii::$app->user->id);

        if (!$customer->validatePassword($form->passwordOld)) {
            throw new BadRequestHttpException("Jelenlegi jelszó nem egyezik", "2");
        }

        $customer->setPassword($form->password);

        $customer->save();
    }

}
