Skip to content

Commit

Permalink
relation optimizations / updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Jun 17, 2019
1 parent 0f5a88f commit a06987c
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 549 deletions.
16 changes: 8 additions & 8 deletions src/actions/organizations/PopulateOrganizationTrait.php
Expand Up @@ -72,14 +72,6 @@ protected function populateFromRequest(OrganizationElement $organization)
// Join date
$this->populateDateFromRequest($organization, 'dateJoined');

// Active type
$type = Craft::$app->getRequest()->getParam('type');
if (!empty($type)) {
$organization->setActiveType(
OrganizationType::getOne($type)
);
}

// Set types
$organization->setTypesFromRequest(
(string)$request->getParam('typesLocation', 'types')
Expand All @@ -94,6 +86,14 @@ protected function populateFromRequest(OrganizationElement $organization)
$organization->setFieldValuesFromRequest(
(string)$request->getParam('fieldsLocation', 'fields')
);

// Active type
$type = Craft::$app->getRequest()->getParam('type');
if (!empty($type)) {
$organization->setActiveType(
OrganizationType::getOne($type)
);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/elements/TypesAttributeTrait.php
Expand Up @@ -47,7 +47,7 @@ trait TypesAttributeTrait
public function setTypesFromRequest(string $identifier = 'types')
{
if (null !== ($types = Craft::$app->getRequest()->getBodyParam($identifier))) {
$this->getTypes()->add($types);
$this->getTypes()->clear()->add($types);
}

return $this;
Expand Down
42 changes: 0 additions & 42 deletions src/models/OrganizationTypeAttributeTrait.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/models/OrganizationTypeRulesTrait.php

This file was deleted.

42 changes: 0 additions & 42 deletions src/models/UserTypeAttributeTrait.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/models/UserTypeRulesTrait.php

This file was deleted.

39 changes: 39 additions & 0 deletions src/objects/OrganizationTypeAttributeTrait.php
Expand Up @@ -8,6 +8,8 @@

namespace flipbox\organizations\objects;

use flipbox\organizations\records\OrganizationType;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
* @since 1.0.0
Expand All @@ -16,8 +18,45 @@ trait OrganizationTypeAttributeTrait
{
use OrganizationTypeMutatorTrait;

/**
* @var OrganizationType|null
*/
private $type;

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

/**
* @param int|null $id
*/
protected function internalSetTypeId(int $id = null)
{
$this->typeId = $id;
}

/**
* @return int|null
*/
protected function internalGetTypeId()
{
return $this->typeId;
}

/**
* @param OrganizationType|null $type
*/
protected function internalSetType(OrganizationType $type = null)
{
$this->type = $type;
}

/**
* @return OrganizationType|null
*/
protected function internalGetType()
{
return $this->type;
}
}
62 changes: 35 additions & 27 deletions src/objects/OrganizationTypeMutatorTrait.php
Expand Up @@ -19,49 +19,59 @@
trait OrganizationTypeMutatorTrait
{
/**
* @var OrganizationType|null
* @param int|null $id
*/
private $type;
abstract protected function internalSetTypeId(int $id = null);

/**
* @return int|null
*/
abstract protected function internalGetTypeId();

/**
* @param OrganizationType|null $type
*/
abstract protected function internalSetType(OrganizationType $type = null);

/**
* @return OrganizationType|null
*/
abstract protected function internalGetType();

/**
* Set associated typeId
*
* @param $id
* @return $this
*/
public function setTypeId(int $id)
public function setTypeId(int $id = null)
{
$this->typeId = $id;
$this->internalSetTypeId($id);
return $this;
}

/**
* Get associated typeId
*
* @return int|null
*/
public function getTypeId()
{
if (null === $this->typeId && null !== $this->type) {
$this->typeId = $this->type->id;
if (null === $this->internalGetTypeId() && null !== $this->internalGetType()) {
$this->setTypeId($this->internalGetType()->id);
}

return $this->typeId;
return $this->internalGetTypeId();
}

/**
* @param $type
* @return static
* @return $this
*/
public function setType($type = null)
{
$this->type = null;
$this->internalSetType(null);
$this->internalSetTypeId(null);

if (!$type = $this->internalResolveType($type)) {
$this->type = $this->typeId = null;
} else {
$this->typeId = $type->id;
$this->type = $type;
if ($type = $this->internalResolveType($type)) {
$this->internalSetType($type);
$this->internalSetTypeId($type->id);
}

return $this;
Expand All @@ -72,21 +82,19 @@ public function setType($type = null)
*/
public function getType()
{
if ($this->type === null) {
if ($this->internalGetType() === null) {
$type = $this->resolveType();
$this->setType($type);
return $type;
}

$typeId = $this->typeId;
if ($typeId !== null &&
$typeId !== $this->type->id
) {
$this->type = null;
$typeId = $this->internalGetTypeId();
if ($typeId !== null && $typeId !== $this->internalGetType()->id) {
$this->internalSetType(null);
return $this->getType();
}

return $this->type;
return $this->internalGetType();
}

/**
Expand All @@ -106,11 +114,11 @@ protected function resolveType()
*/
private function resolveTypeFromId()
{
if (null === $this->typeId) {
if (null === $this->internalGetTypeId()) {
return null;
}

return OrganizationType::findOne($this->typeId);
return OrganizationType::findOne($this->internalGetTypeId());
}

/**
Expand Down

0 comments on commit a06987c

Please sign in to comment.