Skip to content

Commit

Permalink
[WIP][unitfactoryAlumni#66] Added more abstractions to RememberHelper…
Browse files Browse the repository at this point in the history
…\RememberHelper. Seems like all are working now
  • Loading branch information
vbrazas committed Apr 1, 2019
1 parent 1bbb385 commit 164d49f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 58 deletions.
23 changes: 15 additions & 8 deletions helpers/RememberUserInfo/RememberAchievements.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@
class RememberAchievements extends RememberHelper
{

protected function norminateTheResponse()
protected function init()
{
foreach ($this->response['achievements'] as &$achievement) {
$achievement['xlogin'] = $this->response['login'];
$this->responseSubSet =& $this->response['projects_users'];
$this->model = new Achievements();
}

protected function norminate()
{
$this->responseSubSet =& $this->response['achievements'];

foreach ($this->responseSubSet as &$achievement) {
$this->setXlogin($achievement);
$achievement['visible'] = $achievement['visible'] ? 'True' : 'False';
self::swapKeysInArr($achievement, [ 'id' => 'aid' ]);
}
}

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

foreach ($this->response['achievements'] as &$achievement) {
self::saveChangesToDB($achievements, $achievement, $achievements->find()
foreach ($this->responseSubSet as &$achievement) {
self::saveChangesToDB($this->model, $achievement, $this->model->find()
->Where([ 'aid' => $achievement['aid'] ])
->andWhere([ 'xlogin' => $achievement['xlogin'] ])
->all());
Expand Down
20 changes: 13 additions & 7 deletions helpers/RememberUserInfo/RememberCurses.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
class RememberCurses extends RememberHelper
{

protected function norminateTheResponse()
protected function init()
{
foreach ($this->response['cursus_users'] as &$curs) {
$this->responseSubSet =& $this->response['projects_users'];
$this->model = new Curses();
}

protected function norminate()
{
$this->responseSubSet =& $this->response['cursus_users'];

foreach ($this->responseSubSet as &$curs) {
self::dateToSqlFormat($curs['begin_at']);
self::swapKeysInArr($curs, [ 'id' => 'xid' ]);
self::mergeChildArrByKey($curs, 'cursus');
Expand All @@ -20,12 +28,10 @@ protected function norminateTheResponse()
}
}

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

foreach ($this->response['cursus_users'] as &$curs) {
self::saveChangesToDB($curses, $curs, $curses->find()
foreach ($this->responseSubSet as &$curs) {
self::saveChangesToDB($this->model, $curs, $this->model->find()
->Where([ 'xlogin' => $curs['xlogin'] ])
->andWhere([ 'cursus_id' => $curs['cursus_id'] ])
->all());
Expand Down
26 changes: 19 additions & 7 deletions helpers/RememberUserInfo/RememberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,37 @@
abstract class RememberHelper
{

abstract public function rememberToDB();
abstract protected function norminateTheResponse();
abstract protected function init();
abstract protected function remember();
abstract protected function norminate();


protected $response;

protected $responseSubSet;
protected $model;


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

$this->norminate();
$this->remember();
}


protected function setXlogin(&$arrToSetXlogin, $xloginKey = 'xlogin')
{
$arrToSetXlogin[$xloginKey] = $this->response['login'];
}


protected static function isArraysIdentical($a1, $a2, $arrayKeysToCompare)
{
foreach ($arrayKeysToCompare as $keyFor_both) {
if ($a1[$keyFor_both] != $a2[$keyFor_both]) {
foreach ($arrayKeysToCompare as $keyForBoth) {
if ($a1[$keyForBoth] != $a2[$keyForBoth]) {
return false;
}
}
Expand Down Expand Up @@ -70,7 +82,7 @@ protected static function saveChangesToDB($baseActiveRecordModel, $arrToPutIntoD

if ( $identicalNotFound && $index == $len ) {
$AR->attributes = $arrToPutIntoDb;
$AR->update(false);
$AR->save(false);
} else {
$AR->delete();
}
Expand Down
5 changes: 3 additions & 2 deletions helpers/RememberUserInfo/RememberLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
class RememberLevel extends RememberHelper
{

protected function norminateTheResponse() { }
protected function init() { }
protected function norminate() { }

public function rememberToDB()
protected function remember()
{
Yii::$app->session->set('level', $this->response['cursus_users'][0]['level']);
}
Expand Down
21 changes: 13 additions & 8 deletions helpers/RememberUserInfo/RememberProjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
class RememberProjects extends RememberHelper
{

protected function norminateTheResponse()
protected function init()
{
foreach ($this->response['projects_users'] as &$project) {
$project['xlogin'] = $this->response['login'];
$this->responseSubSet =& $this->response['projects_users'];
$this->model = new ProjectsAll();
}

protected function norminate()
{

foreach ($this->responseSubSet as &$project) {
$this->setXlogin($project);
$project['cursus_ids'] = $project['cursus_ids'][0]; // ??? WTF
$project['validated?'] = $project['validated?'] ? 'True' : 'False';
// $project['marked'] = $project['marked'] ? 'True' : 'False';
Expand All @@ -22,12 +29,10 @@ protected function norminateTheResponse()
}
}

public function rememberToDB()
protected function remember()
{
$pusers = new ProjectsAll();

foreach ($this->response['projects_users'] as &$project) {
self::saveChangesToDB($pusers, $project, $pusers->find()
foreach ($this->responseSubSet as &$project) {
self::saveChangesToDB($this->model, $project, $this->model->find()
->Where([ 'puid' => $project['puid'] ])
->andWhere([ 'xlogin' => $project['xlogin'] ])
->all());
Expand Down
22 changes: 15 additions & 7 deletions helpers/RememberUserInfo/RememberShow.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
class RememberShow extends RememberHelper
{

protected function norminateTheResponse()
protected function init()
{
self::swapKeysInArr($this->response, [ 'id' => 'xid' ]);
$this->responseSubSet =& $this->response['projects_users'];
$this->model = new Show();
}

public function rememberToDB()
protected function norminate()
{
$show = new Show();
$this->responseSubSet =& $this->response;

self::saveChangesToDB($show, $this->response, $show->find()
->Where([ 'xid' => $this->response['xid'] ])
->orWhere([ 'login' => $this->response['login'] ])
$this->responseSubSet['kick'] = 0; // ??? WTF
$this->responseSubSet['lastloc'] = date('Y-m-d H:i:s'); // ??? WTF
self::swapKeysInArr($this->responseSubSet, [ 'id' => 'xid' ]);
}

protected function remember()
{
self::saveChangesToDB($this->model, $this->responseSubSet, $this->model->find()
->Where([ 'xid' => $this->responseSubSet['xid'] ])
->orWhere([ 'login' => $this->responseSubSet['login'] ])
->all());
}

Expand Down
24 changes: 15 additions & 9 deletions helpers/RememberUserInfo/RememberSkills.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@
class RememberSkills extends RememberHelper
{

protected function norminateTheResponse()
protected function init()
{
foreach ($this->response['cursus_users'] as &$cursus) {
$this->responseSubSet =& $this->response['projects_users'];
$this->model = new Skills();
}

protected function norminate()
{
$this->responseSubSet =& $this->response['cursus_users'];

foreach ($this->responseSubSet as &$cursus) {
foreach ($cursus['skills'] as &$skill) {
$skill['xlogin'] = $this->response['login'];
$skill['cursus_id'] = $cursus['skills']['cursus_id'];
$this->setXlogin($skill);
$skill['cursus_id'] = $cursus['cursus_id'];
self::swapKeysInArr($skill, [ 'id' => 'skills_id', 'name' => 'skills_name', 'level' => 'skills_level' ]);
}
}
}

public function rememberToDB()
protected function remember()
{
$skills = new Skills();

foreach ($this->response['cursus_users'] as &$cursus) {
foreach ($this->responseSubSet as &$cursus) {
foreach ($cursus['skills'] as &$skill) {
self::saveChangesToDB($skills, $skill, $skills->find()
self::saveChangesToDB($this->model, $skill, $this->model->find()
->Where([ 'skills_id' => $skill['skills_id'] ])
->andWhere([ 'xlogin' => $skill['xlogin'] ])
->all());
Expand Down
1 change: 1 addition & 0 deletions helpers/RememberUserInfo/RememberUserInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ public static function rememberAllToDB($response)
}

}
// WHERE `xlogin`='vbrazas'
10 changes: 0 additions & 10 deletions models/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ public function rules()
];
}

/**
* {@inheritdoc}
*/
public function init()
{
$this->kick = 0; // ??? WTF
$this->lastloc = date('Y-m-d H:i:s'); // ??? WTF
parent::init();
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 164d49f

Please sign in to comment.