Skip to content

Commit

Permalink
attribute optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Jul 19, 2019
1 parent 05d944d commit aa64cd6
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 60 deletions.
Expand Up @@ -14,7 +14,7 @@
use craft\elements\db\ElementQueryInterface;
use craft\elements\db\UserQuery;
use craft\helpers\ArrayHelper;
use flipbox\organizations\objects\OrganizationMutatorTrait;
use flipbox\organizations\objects\OrganizationAttributeTrait;
use yii\base\InvalidArgumentException;

/**
Expand All @@ -23,7 +23,7 @@
*/
class DissociateUsersFromOrganizationAction extends ElementAction
{
use OrganizationMutatorTrait;
use OrganizationAttributeTrait;

/**
* @return array
Expand Down
4 changes: 2 additions & 2 deletions src/elements/actions/EditUserAssociation.php
Expand Up @@ -11,15 +11,15 @@
use Craft;
use craft\base\ElementAction;
use craft\helpers\Json;
use flipbox\organizations\objects\OrganizationMutatorTrait;
use flipbox\organizations\objects\OrganizationAttributeTrait;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
* @since 1.0.0
*/
class EditUserAssociation extends ElementAction
{
use OrganizationMutatorTrait;
use OrganizationAttributeTrait;

/**
* @return array
Expand Down
3 changes: 2 additions & 1 deletion src/models/DateJoinedAttributeTrait.php
Expand Up @@ -11,6 +11,7 @@
use Craft;
use DateTime;
use flipbox\organizations\objects\DateJoinedMutatorTrait;
use flipbox\organizations\Organizations;

/**
* @property DateTime|null $dateJoined
Expand Down Expand Up @@ -43,7 +44,7 @@ protected function dateJoinedAttributes(): array
protected function dateJoinedAttributeLabels()
{
return [
'dateJoined' => Craft::t('organizations', 'Date Joined')
'dateJoined' => Organizations::t('Date Joined')
];
}
}
4 changes: 2 additions & 2 deletions src/models/DateJoinedRulesTrait.php
Expand Up @@ -10,7 +10,7 @@

use craft\validators\DateTimeValidator;
use DateTime;
use flipbox\craft\ember\helpers\ModelHelper;
use yii\base\Model;

/**
* @property DateTime|null $dateJoined
Expand Down Expand Up @@ -38,7 +38,7 @@ protected function dateJoinedRules()
],
'safe',
'on' => [
ModelHelper::SCENARIO_DEFAULT
Model::SCENARIO_DEFAULT
]
]
];
Expand Down
40 changes: 40 additions & 0 deletions src/models/OrganizationAttributeTrait.php
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (c) Flipbox Digital Limited
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
* @link https://github.com/flipboxfactory/craft-ember
*/

namespace flipbox\organizations\models;

use flipbox\organizations\Organizations;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
* @since 2.0.0
*/
trait OrganizationAttributeTrait
{
use OrganizationRulesTrait, \flipbox\organizations\objects\OrganizationAttributeTrait;

/**
* @return array
*/
protected function organizationAttributes(): array
{
return [
'organizationId'
];
}

/**
* @return array
*/
protected function organizationAttributeLabels(): array
{
return [
'organizationId' => Organizations::t('Organization Id')
];
}
}
4 changes: 2 additions & 2 deletions src/models/OrganizationRulesTrait.php
Expand Up @@ -9,7 +9,7 @@
namespace flipbox\organizations\models;

use craft\elements\User as UserElement;
use flipbox\craft\ember\helpers\ModelHelper;
use yii\base\Model;

/**
* @property int|null $userId
Expand Down Expand Up @@ -40,7 +40,7 @@ protected function organizationRules(): array
],
'safe',
'on' => [
ModelHelper::SCENARIO_DEFAULT
Model::SCENARIO_DEFAULT
]
]
];
Expand Down
42 changes: 42 additions & 0 deletions src/objects/OrganizationAttributeTrait.php
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (c) Flipbox Digital Limited
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
* @link https://github.com/flipboxfactory/craft-ember
*/

namespace flipbox\organizations\objects;

/**
* @property int|null $organizationId
*
* @author Flipbox Factory <hello@flipboxfactory.com>
* @since 1.0.0
*/
trait OrganizationAttributeTrait
{
use OrganizationMutatorTrait;

/**
* @var int|null
*/
private $organizationId;

/**
* @inheritDoc
*/
protected function internalSetOrganizationId(int $id = null)
{
$this->organizationId = $id;
return $this;
}

/**
* @inheritDoc
*/
protected function internalGetOrganizationId()
{
return $this->organizationId === null ? null : (int) $this->organizationId;
}
}
85 changes: 49 additions & 36 deletions src/objects/OrganizationMutatorTrait.php
Expand Up @@ -13,7 +13,13 @@
use flipbox\organizations\elements\Organization;

/**
* @property int|null $organizationId
* This trait accepts both an Organization or and Organization Id and ensures that the both
* the Organization and the Id are in sync; If one changes (and does not match the other) it
* resolves (removes / updates) the other.
*
* In addition, this trait is primarily useful when a new Organization is set and saved; the Organization
* Id can be retrieved without needing to explicitly set the newly created Id.
*
* @property Organization|null $organization
*
* @author Flipbox Factory <hello@flipboxfactory.com>
Expand All @@ -26,6 +32,23 @@ trait OrganizationMutatorTrait
*/
private $organization;

/**
* Internally set the Organization Id. This can be overridden. A record for example
* should use `setAttribute`.
*
* @param int|null $id
* @return $this
*/
abstract protected function internalSetOrganizationId(int $id = null);

/**
* Internally get the Organization Id. This can be overridden. A record for example
* should use `getAttribute`.
*
* @return int|null
*/
abstract protected function internalGetOrganizationId();

/**
* @return bool
*/
Expand All @@ -40,9 +63,14 @@ public function isOrganizationSet(): bool
* @param $id
* @return $this
*/
public function setOrganizationId(int $id)
public function setOrganizationId(int $id = null)
{
$this->organizationId = $id;
$this->internalSetOrganizationId($id);

if (null !== $this->organization && $id !== $this->organization->id) {
$this->organization = null;
}

return $this;
}

Expand All @@ -53,11 +81,11 @@ public function setOrganizationId(int $id)
*/
public function getOrganizationId()
{
if (null === $this->organizationId && null !== $this->organization) {
$this->organizationId = $this->organization->id;
if (null === $this->internalGetOrganizationId() && null !== $this->organization) {
$this->setOrganizationId($this->organization->id);
}

return $this->organizationId;
return $this->internalGetOrganizationId();
}

/**
Expand All @@ -69,12 +97,11 @@ public function getOrganizationId()
public function setOrganization($organization = null)
{
$this->organization = null;
$this->internalSetOrganizationId(null);

if (null === ($organization = $this->internalResolveOrganization($organization))) {
$this->organization = $this->organizationId = null;
} else {
$this->organizationId = $organization->id;
if (null !== ($organization = $this->verifyOrganization($organization))) {
$this->organization = $organization;
$this->internalSetOrganizationId($organization->id);
}

return $this;
Expand All @@ -91,10 +118,8 @@ public function getOrganization()
return $organization;
}

$organizationId = $this->organizationId;
if ($organizationId !== null &&
$organizationId !== $this->organization->id
) {
$organizationId = $this->internalGetOrganizationId();
if ($organizationId !== null && $organizationId !== $this->organization->id) {
$this->organization = null;
return $this->getOrganization();
}
Expand All @@ -107,8 +132,8 @@ public function getOrganization()
*/
protected function resolveOrganization()
{
if ($model = $this->resolveOrganizationFromId()) {
return $model;
if ($organization = $this->resolveOrganizationFromId()) {
return $organization;
}

return null;
Expand All @@ -119,18 +144,21 @@ protected function resolveOrganization()
*/
private function resolveOrganizationFromId()
{
if (null === $this->organizationId) {
if (null === ($organizationId = $this->internalGetOrganizationId())) {
return null;
}

return Organization::findOne($this->organizationId);
return Organization::findOne($organizationId);
}

/**
* @param $organization
* Attempt to verify that the passed 'organization' is a valid element. A primary key or query
* can be passed to lookup an organization.
*
* @param mixed $organization
* @return Organization|null
*/
protected function internalResolveOrganization($organization = null)
protected function verifyOrganization($organization = null)
{
if (null === $organization) {
return null;
Expand All @@ -140,21 +168,6 @@ protected function internalResolveOrganization($organization = null)
return $organization;
}

if (is_numeric($organization) || is_string($organization)) {
return Organization::findOne($organization);
}

try {
$object = Craft::createObject(Organization::class, [$organization]);
} catch (\Exception $e) {
$object = new Organization();
ObjectHelper::populate(
$object,
$organization
);
}

/** @var Organization $object */
return $object;
return Organization::findOne($organization);
}
}

0 comments on commit aa64cd6

Please sign in to comment.