Skip to content

Commit

Permalink
[WIP][unitfactoryAlumni#66] All code refactored, created new namespac…
Browse files Browse the repository at this point in the history
…e: app\helpers\RememberUserInfo; Achievements RememberHelper added; fixed couple of inaccuracies in view/site/login.php, view/layouts/main.php
  • Loading branch information
vbrazas committed Apr 6, 2019
1 parent a08599b commit dfe91e9
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 194 deletions.
4 changes: 3 additions & 1 deletion helpers/Auth42.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace app\helpers;

// use RememberUserInfo\RememberUserInfo;

use Yii;
use yii\authclient\InvalidResponseException;
use yii\authclient\OAuth2;
Expand Down Expand Up @@ -83,7 +85,7 @@ public function fetchMe($params)
->setHeaders($params);
$response = $this->sendRequest($request);

(new RememberUserInfo($response))->rememberAll();
RememberUserInfo\RememberUserInfo::rememberAllToDB($response);

$profileLink = '/pools/';
if (isset($response['cursus_users'])) {
Expand Down
187 changes: 0 additions & 187 deletions helpers/RememberUserInfo.php

This file was deleted.

30 changes: 30 additions & 0 deletions helpers/RememberUserInfo/RememberAchievements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace app\helpers\RememberUserInfo;

use app\models\Achievements;

class RememberAchievements extends RememberHelper
{

protected function norminateTheResponse()
{
foreach ($this->response['achievements'] as &$achievement) {
$achievement['xlogin'] = $this->response['login'];
self::swapKeysInArr($achievement, [ 'id' => 'aid' ]);
}
}

public function rememberToDB()
{
$achievements = new Achievements();

foreach ($this->response['achievements'] as &$achievement) {
self::saveChangesToDB($achievements, $achievement, $achievements->find()
->Where([ 'aid' => $achievement['aid'] ])
->andWhere([ 'xlogin' => $achievement['xlogin'] ])
->all());
}
}

}
35 changes: 35 additions & 0 deletions helpers/RememberUserInfo/RememberCurses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace app\helpers\RememberUserInfo;

use app\models\Curses;

class RememberCurses extends RememberHelper
{

protected function norminateTheResponse()
{
foreach ($this->response['cursus_users'] as &$curs) {
self::dateToSqlFormat($curs['begin_at']);
self::swapKeysInArr($curs, [ 'id' => 'xid' ]);
self::mergeChildArrByKey($curs, 'cursus');
unset($curs['id']);
unset($curs['user']);
$curs['has_coalition'] = $curs['has_coalition'] ? 'True' : 'False';
self::dateToSqlFormat($curs['created_at']);
}
}

public function rememberToDB()
{
$curses = new Curses();

foreach ($this->response['cursus_users'] as &$curs) {
self::saveChangesToDB($curses, $curs, $curses->find()
->Where([ 'xlogin' => $curs['xlogin'] ])
->andWhere([ 'cursus_id' => $curs['cursus_id'] ])
->all());
}
}

}
80 changes: 80 additions & 0 deletions helpers/RememberUserInfo/RememberHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace app\helpers\RememberUserInfo;

abstract class RememberHelper
{

abstract public function rememberToDB();
abstract protected function norminateTheResponse();


protected $response;


public function __construct(&$response)
{
$this->response =& $response;
static::norminateTheResponse();
static::rememberToDB();
}


protected static function isArraysIdentical($a1, $a2, $arrayKeysToCompare)
{
foreach ($arrayKeysToCompare as $keyFor_both) {
if ($a1[$keyFor_both] != $a2[$keyFor_both]) {
return false;
}
}
return true;
}

protected static function dateToSqlFormat(&$date)
{
$date = date('Y-m-d H:i:s', strtotime( $date ));
}

protected static function swapKeysInArr(&$arrToChangeKeys, $keys)
{
foreach ($keys as $keyToReplace => $keyToPut) {
$arrToChangeKeys[$keyToPut] = $arrToChangeKeys[$keyToReplace];
unset($arrToChangeKeys[$keyToReplace]);
}
}

protected static function mergeChildArrByKey(&$arr, $key)
{
foreach ($arr[$key] as $key => $val) {
$arr[$key] = $val;
}
unset($arr[$key]);
}


protected static function saveChangesToDB($baseActiveRecordModel, $arrToPutIntoDb, $activeRecords)
{
if ( empty($activeRecords) ) {
$baseActiveRecordModel->attributes = $arrToPutIntoDb;
$baseActiveRecordModel->insert(false);
}

$identicalNotFound = true;
$len = sizeof($activeRecords) - 1;

foreach ($activeRecords as $index => $AR) {
if ( static::isArraysIdentical($arrToPutIntoDb, $AR, $AR::attributes()) ) {
$identicalNotFound = false;
continue ;
}

if ( $identicalNotFound && $index == $len ) {
$AR->attributes = $arrToPutIntoDb;
$AR->update(false);
} else {
$AR->delete();
}
}
}

}
17 changes: 17 additions & 0 deletions helpers/RememberUserInfo/RememberLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace app\helpers\RememberUserInfo;

use Yii;

class RememberLevel extends RememberHelper
{

protected function norminateTheResponse() { }

public function rememberToDB()
{
Yii::$app->session->set('level', $this->response['cursus_users'][0]['level']);
}

}
Loading

0 comments on commit dfe91e9

Please sign in to comment.