Skip to content

Commit

Permalink
making actions more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Nov 18, 2019
1 parent c5e17bf commit b595d40
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
25 changes: 25 additions & 0 deletions src/actions/organizations/AssociateUserToOrganization.php
Expand Up @@ -13,6 +13,7 @@
use flipbox\organizations\records\UserAssociation;
use yii\base\Action;
use yii\db\ActiveRecord;
use yii\web\HttpException;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
Expand All @@ -30,6 +31,30 @@ class AssociateUserToOrganization extends Action
'state'
];

/**
* @param string $user
* @param string $organization
* @return null|\yii\base\Model|\yii\web\Response
* @throws HttpException
*/
public function run(
string $user,
string $organization
)
{
if (null === ($user = $this->findUser($user))) {
return $this->handleNotFoundResponse();
}

if (null === ($organization = $this->findOrganization($organization))) {
return $this->handleNotFoundResponse();
}

return $this->runInternal(
$organization->getUsers()->findOrCreate($user)
);
}

/**
* @inheritdoc
* @param UserAssociation $record
Expand Down
25 changes: 25 additions & 0 deletions src/actions/organizations/DissociateUserFromOrganization.php
Expand Up @@ -11,6 +11,7 @@
use flipbox\craft\ember\actions\records\DeleteRecordTrait;
use flipbox\organizations\records\UserAssociation;
use yii\base\Action;
use yii\web\HttpException;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
Expand All @@ -20,6 +21,30 @@ class DissociateUserFromOrganization extends Action
{
use DeleteRecordTrait, LookupAssociationTrait;

/**
* @param string $user
* @param string $organization
* @return null|\yii\base\Model|\yii\web\Response
* @throws HttpException
*/
public function run(
string $user,
string $organization
)
{
if (null === ($user = $this->findUser($user))) {
return $this->handleNotFoundResponse();
}

if (null === ($organization = $this->findOrganization($organization))) {
return $this->handleNotFoundResponse();
}

return $this->runInternal(
$organization->getUsers()->findOrCreate($user)
);
}

/**
* @inheritdoc
* @param UserAssociation $record
Expand Down
38 changes: 8 additions & 30 deletions src/actions/organizations/LookupAssociationTrait.php
Expand Up @@ -11,7 +11,6 @@
use Craft;
use craft\elements\User;
use flipbox\organizations\elements\Organization;
use flipbox\organizations\records\UserAssociation;
use yii\db\ActiveRecord;
use yii\web\HttpException;

Expand All @@ -23,35 +22,6 @@
*/
trait LookupAssociationTrait
{
/**
* @param ActiveRecord|UserAssociation $record
* @return UserAssociation
*/
abstract protected function runInternal(ActiveRecord $record);

/**
* @param string $user
* @param string $organization
* @return null|\yii\base\Model|\yii\web\Response
* @throws HttpException
*/
public function run(
string $user,
string $organization
) {
if (null === ($user = $this->findUser($user))) {
return $this->handleNotFoundResponse();
}

if (null === ($organization = Organization::findOne($organization))) {
return $this->handleNotFoundResponse();
}

return $this->runInternal(
$organization->getUsers()->findOrCreate($user)
);
}

/**
* HTTP not found response code
*
Expand Down Expand Up @@ -82,6 +52,14 @@ protected function handleNotFoundResponse()
);
}

/**
* @param string|int $identifier
* @return ORganization|null
*/
protected function findOrganization($identifier)
{
return Organization::findOne($identifier);
}

/**
* @param string|int $identifier
Expand Down

0 comments on commit b595d40

Please sign in to comment.