Skip to content

Commit

Permalink
Merge pull request #10 from tecnocen-com/typecast
Browse files Browse the repository at this point in the history
Typecast
  • Loading branch information
neverabe authored Jul 14, 2019
2 parents 10883ae + d7df7d9 commit e6a2607
Show file tree
Hide file tree
Showing 35 changed files with 209 additions and 216 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ language: php
php:
- 7.1
- 7.2
- 7.3
- nightly

matrix:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Yii2 Workflow Change Log
==========================

0.4.0 July 13, 2019

- Brk: Methods now use the typecast supported in php 7.1

0.3.2 December 18, 2018

- fix version bump for yii2 roa tests

0.3.1 December 17, 2018
------------------------

Expand Down
8 changes: 3 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@
}
],
"require": {
"php": "^7.0",
"ext-curl": "*",
"tecnocen/yii2-roa": ">0.4.1",
"php": "^7.1",
"tecnocen/yii2-roa": "~0.5.0",
"tecnocen/yii2-rmdb": "*",
"yii2tech/ar-softdelete": "*",
"yiisoft/yii2": "~2.0.10"
"yii2tech/ar-softdelete": "*"
},
"require-dev": {
"codeception/base": "^2.2.11",
Expand Down
8 changes: 5 additions & 3 deletions src/models/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use tecnocen\rmdb\models\Pivot;
use Yii;
use yii\db\ActiveQuery;

/**
* @property int $process_id
Expand All @@ -16,7 +17,7 @@ abstract class Assignment extends Pivot
/**
* @return string class name for the process this worklog is attached to.
*/
protected abstract function processClass();
protected abstract function processClass(): string;

/**
* @inheritdoc
Expand Down Expand Up @@ -65,10 +66,11 @@ public function init()
}
parent::init();
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getProcess()
public function getProcess(): ActiveQuery
{
return $this->hasOne($this->processClass(), ['id' => 'process_id']);
}
Expand Down
74 changes: 41 additions & 33 deletions src/models/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use tecnocen\rmdb\models\Entity;
use yii\base\InvalidConfigException;
use yii\db\ActiveQuery;

/**
* Model class for process which change stages depending on a worklow
Expand Down Expand Up @@ -32,7 +33,7 @@ abstract class Process extends Entity
* @return string full class name of the class to be used for the relation
* `getWorkflow()`.
*/
public function workflowClass()
public function workflowClass(): string
{
return Workflow::class;
}
Expand All @@ -41,18 +42,18 @@ public function workflowClass()
* @return string full class name of the class to be used to store the
* assignment records.
*/
protected abstract function assignmentClass();
protected abstract function assignmentClass(): string;

/**
* @return string full class name of the class to be used to store the
* WorkLog records.
*/
protected abstract function WorkLogClass();
protected abstract function workLogClass(): string;

/**
* @return int the id of the workflow this process belongs to.
*/
public abstract function getWorkflowId();
public abstract function getWorkflowId(): int;

/**
* Determines if the current process record has the need of an initial
Expand All @@ -61,7 +62,7 @@ public abstract function getWorkflowId();
*
* @return bool whether the initial worklog was autogenerated.
*/
private function hasInitialWorkLog()
private function hasInitialWorkLog(): bool
{
if (!$this->autogenerateInitialWorklog || !$this->isNewRecord) {
return false;
Expand Down Expand Up @@ -92,10 +93,10 @@ public function load($data, $formName = null)
/**
* Whether the user is asigned to the process.
*
* @param int $userId [description]
* @return boolean
* @param ?int $userId
* @return bool
*/
public function userAssigned($userId)
public function userAssigned(?int $userId): bool
{
return !$this->getAssignments()->exists() || $this->getAssignments()
->andWhere(['user_id' => $userId]);
Expand Down Expand Up @@ -151,9 +152,9 @@ public function afterSave($insert, $changedAttributes)
*/
public function init()
{
if (!is_subclass_of($this->WorkLogClass(), WorkLog::class)) {
if (!is_subclass_of($this->workLogClass(), WorkLog::class)) {
throw new InvalidConfigException(
static::class . '::WorkLogClass() must extend '
static::class . '::workLogClass() must extend '
. WorkLog::class
);
}
Expand All @@ -169,45 +170,45 @@ public function init()
/**
* @return Workflow
*/
public function getWorkflow()
public function getWorkflow(): Workflow
{
$workflowClass = $this->workflowClass();

return $workflowClass::findOne($this->getWorkflowId());
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getAssignments()
public function getAssignments(): ActiveQuery
{
return $this->hasMany($this->assignmentClass(), [
'process_id' => 'id',
])->inverseOf('process');
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getWorkLogs()
public function getWorkLogs(): ActiveQuery
{
return $this->hasMany($this->WorkLogClass(), [
return $this->hasMany($this->workLogClass(), [
'process_id' => 'id',
])->inverseOf('process');
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
* @see https://dev.mysql.com/doc/refman/5.7/en/example-maximum-column-group-row.html
*/
public function getActiveWorkLog()
public function getActiveWorkLog(): ActiveQuery
{
$query = $this->getWorkLogs()->alias('WorkLog');
$query->multiple = false;
$WorkLogClass = $this->WorkLogClass();
$workLogClass = $this->workLogClass();

return $query->andWhere([
'id' => $WorkLogClass::find()
'id' => $workLogClass::find()
->select(['MAX(id)'])
->alias('WorkLog_groupwise')
->andWhere('WorkLog.process_id = WorkLog_groupwise.process_id')
Expand All @@ -222,29 +223,36 @@ public function getActiveWorkLog()
* to create said WorkLog.
* @param bool $runValidation
*/
public function flow(&$WorkLog, $runValidation = true)
public function flow(&$workLog, bool $runValidation = true)
{
$WorkLog = $this->ensureWorkLog($WorkLog);
$WorkLog->scenario = WorkLog::SCENARIO_FLOW;
$workLog = $this->ensureWorkLog($workLog);
$workLog->scenario = WorkLog::SCENARIO_FLOW;

return $WorkLog->save($runValidation);
return $workLog->save($runValidation);
}

private function ensureWorkLog($WorkLog)
/**
* Ensures that the provided parameter is either an array to create a valid
* instance of the `workLogClass()` class.
*
* @param array|WorkLog $workLog
* @return WorkLog extending the class defined in `workLogClass()`
*/
private function ensureWorkLog($workLog): WorkLog
{
$WorkLogClass = $this->WorkLogClass();
if (is_array($WorkLog)) {
$WorkLog = new $WorkLogClass($WorkLog);
} elseif (!$WorkLog instanceof $WorkLogClass) {
$workLogClass = $this->workLogClass();
if (is_array($workLog)) {
$workLog = new $workLogClass($workLog);
} elseif (!$workLog instanceof $workLogClass) {
throw new InvalidParamException(
"Parameter must be an instance of {$WorkLogClass} or an "
"Parameter must be an instance of {$workLogClass} or an "
. 'array to configure an instance'
);
}

$WorkLog->process_id = $this->id;
$WorkLog->populateRelation('process', $this);
$workLog->process_id = $this->id;
$workLog->populateRelation('process', $this);

return $WorkLog;
return $workLog;
}
}
21 changes: 12 additions & 9 deletions src/models/Stage.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

namespace tecnocen\workflow\models;

use yii\db\ActiveQuery;

/**
* Model class for table `{{%workflow_stage}}`
*
Expand Down Expand Up @@ -89,9 +92,9 @@ public function attributeLabels()
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getWorkflow()
public function getWorkflow(): ActiveQuery
{
return $this->hasOne(
$this->workflowClass,
Expand All @@ -100,9 +103,9 @@ public function getWorkflow()
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getTransitions()
public function getTransitions(): ActiveQuery
{
return $this->hasMany(
$this->transitionClass,
Expand All @@ -111,9 +114,9 @@ public function getTransitions()
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getDetailTransitions()
public function getDetailTransitions(): ActiveQuery
{
$query = $this->getTransitions();
$query->multiple = false;
Expand All @@ -129,15 +132,15 @@ public function getDetailTransitions()
/**
* @return int
*/
public function getTotalTransitions()
public function getTotalTransitions(): int
{
return (int)$this->detailTransitions['totalTransitions'];
}

/**
* @return \yii\db\ActiveQuery sibling stages for the same workflow
* @return ActiveQuery sibling stages for the same workflow
*/
public function getSiblings()
public function getSiblings(): ActiveQuery
{
return $this->hasMany(static::class, ['workflow_id' => 'workflow_id'])
->alias('siblings');
Expand Down
18 changes: 10 additions & 8 deletions src/models/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace tecnocen\workflow\models;

use Yii;
use yii\db\ActiveQuery;

/**
* Model class for table `{{%workflow_transition}}`
Expand Down Expand Up @@ -126,13 +127,14 @@ public function rules()
*
* @param int $userId
* @param Process $process
* @return boolean
* @return bool
*/
public function userCan($userId, Process $process)
public function userCan(?int $userId, Process $process): bool
{
if (!$this->getPermissions()->exists()) {
return true;
}

$authManager = Yii::$app->authManager;

foreach ($this->permissions as $permission) {
Expand Down Expand Up @@ -160,25 +162,25 @@ public function attributeLabels()
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getSourceStage()
public function getSourceStage(): ActiveQuery
{
return $this->hasOne($this->stageClass, ['id' => 'source_stage_id']);
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getTargetStage()
public function getTargetStage(): ActiveQuery
{
return $this->hasOne($this->stageClass, ['id' => 'target_stage_id']);
}

/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getPermissions()
public function getPermissions(): ActiveQuery
{
return $this->hasMany(
$this->permissionClass,
Expand Down
Loading

0 comments on commit e6a2607

Please sign in to comment.