Skip to content

Commit

Permalink
refactoring normalizer class
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Mar 31, 2017
1 parent fbc7c82 commit 750795a
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 156 deletions.
183 changes: 28 additions & 155 deletions src/Utils/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,33 @@
namespace Imanghafoori\Widgets\Utils;

use Imanghafoori\Widgets\BaseWidget;
use Imanghafoori\Widgets\Utils\Normalizers\CacheNormalizer;
use Imanghafoori\Widgets\Utils\Normalizers\ControllerNormalizer;
use Imanghafoori\Widgets\Utils\Normalizers\PresenterNormalizer;
use Imanghafoori\Widgets\Utils\Normalizers\TemplateNormalizer;

class Normalizer
{
private $widget;
private $presenterNormalizer;
private $templateNormalizer;

/**
* Normalizer constructor.
* @param TemplateNormalizer $templateNormalizer
* @param CacheNormalizer $cacheNormalizer
* @param PresenterNormalizer $presenterNormalizer
* @param ControllerNormalizer $controllerNormalizer
* @internal param $widget
*/
public function __construct(TemplateNormalizer $templateNormalizer, CacheNormalizer $cacheNormalizer, PresenterNormalizer $presenterNormalizer, ControllerNormalizer $controllerNormalizer)
{
$this->presenterNormalizer = $presenterNormalizer;
$this->controllerNormalizer = $controllerNormalizer;
$this->templateNormalizer = $templateNormalizer;
$this->cacheNormalizer = $cacheNormalizer;
}


public function normalizeWidgetConfig(BaseWidget $widget)
{
Expand All @@ -16,80 +39,15 @@ public function normalizeWidgetConfig(BaseWidget $widget)
}

$this->widget = $widget;
$this->normalizeControllerMethod();
$this->normalizePresenterName();
$this->normalizeTemplateName();
$this->controllerNormalizer->normalizeControllerMethod($widget);
$this->presenterNormalizer->normalizePresenterName($widget);
$this->templateNormalizer->normalizeTemplateName($widget);
$this->cacheNormalizer->normalizeCacheLifeTime($widget);
$this->cacheNormalizer->normalizeCacheTags($widget);
$this->normalizeContextAs();
$this->normalizeCacheLifeTime();
$this->normalizeCacheTags();
$widget->isNormalized = true;
}

/**
* Figures out which method should be called as the controller.
* @return null
*/
private function normalizeControllerMethod()
{
// We decide to call data method on widget object by default.
$controllerMethod = [$this->widget, 'data'];
$ctrlClass = get_class($this->widget);

// If the user has explicitly declared controller class path on widget
// then we decide to call data method on that instead.
if ($this->widget->controller) {
$ctrlClass = $this->widget->controller;
$controllerMethod = ($ctrlClass) . '@data';
}

$this->checkControllerExists($ctrlClass);
$this->checkDataMethodExists($ctrlClass);

$this->widget->controller = $controllerMethod;
}

/**
* Figures out which method should be called as the presenter
* @return null
*/
private function normalizePresenterName()
{
if ($this->widget->presenter !== 'default') {
$presenter = $this->widget->presenter;
$this->checkPresenterExists($presenter);
} else {
$presenter = get_class($this->widget) . 'Presenter';
if (!class_exists($presenter)) {
return $this->widget->presenter = null;
}
}

$this->checkPresentMethodExists($presenter);

$this->widget->presenter = $presenter . '@present';
}

/**
* Figures out which template to render.
* @return null
*/
private function normalizeTemplateName()
{
// class name without namespace.
$className = str_replace('App\\Widgets\\', '', class_basename($this->widget));

// replace slashes with dots
$className = str_replace(['\\', '/'], '.', $className);

if ($this->widget->template === null) {
$this->widget->template = 'Widgets::' . $className . 'View';
}

if (!view()->exists($this->widget->template)) {
throw new \InvalidArgumentException("View file [{$className}View] not found by: '" . class_basename($this->widget) . " '");
}
}

/**
* Figures out what the variable name should be in view file.
* @return null
Expand All @@ -99,89 +57,4 @@ private function normalizeContextAs()
// removes the $ sign.
$this->widget->contextAs = str_replace('$', '', (string)$this->widget->contextAs);
}

/**
* ّFigures out how long the cache life time should be.
* @return null
*/
private function normalizeCacheLifeTime()
{
if ($this->widget->cacheLifeTime === 'env_default') {
$this->widget->cacheLifeTime = (int)(env('WIDGET_DEFAULT_CACHE_LIFETIME', 0));
}

if ($this->widget->cacheLifeTime === 'forever') {
$this->widget->cacheLifeTime = -1;
}
}

/**
* ّFigures out what the cache tags should be.
* @return null
*/
private function normalizeCacheTags()
{
if (!$this->cacheCanUseTags() || !$this->widget->cacheTags) {
return $this->widget->cacheTags = null;
}

if (is_array($this->widget->cacheTags)) {
return $this->widget->cacheTags;
}

if (is_string($this->widget->cacheTags)) {
return $this->widget->cacheTags = [$this->widget->cacheTags];
}

throw new \InvalidArgumentException('Cache Tags should be of type String or Array.');
}

/**
* Determine whether cache tags should be applied or not
* @return bool
*/
private function cacheCanUseTags()
{
return !in_array(env('CACHE_DRIVER', 'file'), ['file', 'database']);
}

/**
* @param $ctrlClass
*/
private function checkControllerExists($ctrlClass)
{
if (!class_exists($ctrlClass)) {
throw new \InvalidArgumentException("Controller class: [{$ctrlClass}] not found.");
}
}

/**
* @param $ctrlClass
*/
private function checkDataMethodExists($ctrlClass)
{
if (!method_exists($ctrlClass, 'data')) {
throw new \InvalidArgumentException("'data' method not found on " . $ctrlClass);
}
}

/**
* @param $presenter
*/
private function checkPresentMethodExists($presenter)
{
if (!method_exists($presenter, 'present')) {
throw new \InvalidArgumentException("'present' method not found on : " . $presenter);
}
}

/**
* @param $presenter
*/
private function checkPresenterExists($presenter)
{
if (!class_exists($presenter)) {
throw new \InvalidArgumentException("Presenter Class [{$presenter}] not found.");
}
}
}
53 changes: 53 additions & 0 deletions src/Utils/Normalizers/CacheNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Imanghafoori\Widgets\Utils\Normalizers;

class CacheNormalizer
{
/**
* ّFigures out how long the cache life time should be.
* @param $widget
* @return null
*/
public function normalizeCacheLifeTime($widget)
{
if ($widget->cacheLifeTime === 'env_default') {
$widget->cacheLifeTime = (int)(env('WIDGET_DEFAULT_CACHE_LIFETIME', 0));
}

if ($widget->cacheLifeTime === 'forever') {
$widget->cacheLifeTime = -1;
}
}

/**
* ّFigures out what the cache tags should be.
* @param $widget
* @return null
*/
public function normalizeCacheTags($widget)
{
if (!$this->cacheCanUseTags() || !$widget->cacheTags) {
return $widget->cacheTags = null;
}

if (is_array($widget->cacheTags)) {
return $widget->cacheTags;
}

if (is_string($widget->cacheTags)) {
return $widget->cacheTags = [$widget->cacheTags];
}

throw new \InvalidArgumentException('Cache Tags should be of type String or Array.');
}

/**
* Determine whether cache tags should be applied or not
* @return bool
*/
private function cacheCanUseTags()
{
return !in_array(env('CACHE_DRIVER', 'file'), ['file', 'database']);
}
}
50 changes: 50 additions & 0 deletions src/Utils/Normalizers/ControllerNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Imanghafoori\Widgets\Utils\Normalizers;

class ControllerNormalizer
{
/**
* Figures out which method should be called as the controller.
* @param $widget
* @return null
*/
public function normalizeControllerMethod($widget)
{
// We decide to call data method on widget object by default.
$controllerMethod = [$widget, 'data'];
$ctrlClass = get_class($widget);

// If the user has explicitly declared controller class path on widget
// then we decide to call data method on that instead.
if ($widget->controller) {
$ctrlClass = $widget->controller;
$controllerMethod = ($ctrlClass) . '@data';
}

$this->checkControllerExists($ctrlClass);
$this->checkDataMethodExists($ctrlClass);

$widget->controller = $controllerMethod;
}

/**
* @param $ctrlClass
*/
private function checkControllerExists($ctrlClass)
{
if (!class_exists($ctrlClass)) {
throw new \InvalidArgumentException("Controller class: [{$ctrlClass}] not found.");
}
}

/**
* @param $ctrlClass
*/
private function checkDataMethodExists($ctrlClass)
{
if (!method_exists($ctrlClass, 'data')) {
throw new \InvalidArgumentException("'data' method not found on " . $ctrlClass);
}
}
}
47 changes: 47 additions & 0 deletions src/Utils/Normalizers/PresenterNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Imanghafoori\Widgets\Utils\Normalizers;

class PresenterNormalizer
{
/**
* Figures out which method should be called as the presenter
* @return null
*/
public function normalizePresenterName($widget)
{
if ($widget->presenter !== 'default') {
$presenter = $widget->presenter;
$this->checkPresenterExists($presenter);
} else {
$presenter = get_class($widget) . 'Presenter';
if (!class_exists($presenter)) {
return $widget->presenter = null;
}
}

$this->checkPresentMethodExists($presenter);

$widget->presenter = $presenter . '@present';
}

/**
* @param $presenter
*/
private function checkPresentMethodExists($presenter)
{
if (!method_exists($presenter, 'present')) {
throw new \InvalidArgumentException("'present' method not found on : " . $presenter);
}
}

/**
* @param $presenter
*/
private function checkPresenterExists($presenter)
{
if (!class_exists($presenter)) {
throw new \InvalidArgumentException("Presenter Class [{$presenter}] not found.");
}
}
}
28 changes: 28 additions & 0 deletions src/Utils/Normalizers/TemplateNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Imanghafoori\Widgets\Utils\Normalizers;

class TemplateNormalizer
{
/**
* Figures out which template to render.
* @param $widget
* @return null
*/
public function normalizeTemplateName($widget)
{
// class name without namespace.
$className = str_replace('App\\Widgets\\', '', class_basename($widget));

// replace slashes with dots
$className = str_replace(['\\', '/'], '.', $className);

if ($widget->template === null) {
$widget->template = 'Widgets::' . $className . 'View';
}

if (!view()->exists($widget->template)) {
throw new \InvalidArgumentException("View file [{$className}View] not found by: '" . class_basename($widget) . " '");
}
}
}
Loading

0 comments on commit 750795a

Please sign in to comment.