Skip to content
This repository has been archived by the owner on Aug 18, 2022. It is now read-only.

Commit

Permalink
added scalar type hints; minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Guite committed Mar 26, 2019
1 parent f271de2 commit 1d19a5c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 108 deletions.
3 changes: 3 additions & 0 deletions AbortStageException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Copyright Zikula Foundation 2014.
*
Expand Down
15 changes: 6 additions & 9 deletions FormHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Copyright Zikula Foundation 2014.
*
Expand All @@ -21,22 +24,16 @@ interface FormHandlerInterface
{
/**
* Returns the FQCN of a Symfony Form Type
*
* @return string
*/
public function getFormType();
public function getFormType(): string;

/**
* Handle results of previously validated form
*
* @param FormInterface $form
* @return boolean
*/
public function handleFormResult(FormInterface $form);
public function handleFormResult(FormInterface $form): bool;

/**
* Returns an array of options applied to the Form.
* @return array
*/
public function getFormOptions();
public function getFormOptions(): array;
}
5 changes: 3 additions & 2 deletions InjectContainerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Copyright Zikula Foundation 2014.
*
Expand All @@ -21,8 +24,6 @@ interface InjectContainerInterface
{
/**
* Require the Symfony Container on instantiation
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container);
}
17 changes: 7 additions & 10 deletions StageInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Copyright Zikula Foundation 2014.
*
Expand All @@ -19,29 +22,23 @@ interface StageInterface
{
/**
* The stage name
*
* @return string
*/
public function getName();
public function getName(): string;

/**
* The stage's full template name, e.g. 'AcmeDemoBundle:Stage:prep.html.twig'
* @return string
*/
public function getTemplateName();
public function getTemplateName(): string;

/**
* Logic to determine if the stage is required or can be skipped
*
* @return boolean
* @throws AbortStageException
*/
public function isNecessary();
public function isNecessary(): bool;

/**
* An array of template parameters required in the stage template
*
* @return array
*/
public function getTemplateParams();
public function getTemplateParams(): array;
}
115 changes: 60 additions & 55 deletions Wizard.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Copyright Zikula Foundation 2014.
*
Expand All @@ -16,33 +19,57 @@
namespace Zikula\Component\Wizard;

use Gedmo\Exception\RuntimeException;
use InvalidArgumentException;
use Symfony\Component\Config\Exception\FileLoaderLoadException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class Wizard
* @package Zikula\Component\Wizard
*/
class Wizard
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @var array
*/
private $stagesByName = [];

/**
* @var array
*/
private $stageOrder = [];

/**
* @var string
*/
private $defaultStage;

/**
* @var string
*/
private $currentStageName;
private $YamlFileLoader;

/**
* @var YamlFileLoader
*/
private $yamlFileLoader;

/**
* @var string
*/
private $warning = '';

/**
* Constructor.
*
* @param ContainerInterface $container
* @param string $path including filename, e.g. my/full/path/to/my/stages.yml
* @throws FileLoaderLoadException
*/
function __construct(ContainerInterface $container, $path)
public function __construct(ContainerInterface $container, string $path)
{
$this->container = $container;
if (!empty($path)) {
Expand All @@ -55,10 +82,9 @@ function __construct(ContainerInterface $container, $path)
/**
* Load the stage definitions from $path
*
* @param string $path including filename, e.g. my/full/path/to/my/stages.yml
* @throws FileLoaderLoadException
*/
public function loadStagesFromYaml($path)
public function loadStagesFromYaml(string $path): void
{
if (!file_exists($path)) {
throw new FileLoaderLoadException('Stage definition file cannot be found.');
Expand All @@ -67,12 +93,14 @@ public function loadStagesFromYaml($path)
if ($pathInfo['extension'] !== 'yml') {
throw new FileLoaderLoadException('Stage definition file must include .yml extension.');
}
$this->stages = []; // empty the stages
if (!isset($this->YamlFileLoader)) {
$this->YamlFileLoader = new YamlFileLoader(new FileLocator($pathInfo['dirname']));

// empty the stages
$this->stagesByName = [];
if (!isset($this->yamlFileLoader)) {
$this->yamlFileLoader = new YamlFileLoader(new FileLocator($pathInfo['dirname']));
}
$this->YamlFileLoader->load($pathInfo['basename']);
$stages = $this->YamlFileLoader->getContent();
$this->yamlFileLoader->load($pathInfo['basename']);
$stages = $this->yamlFileLoader->getContent();
$stages = $stages['stages'];
foreach ($stages as $key => $stageArray) {
$this->stagesByName[$key] = $stageArray['class'];
Expand All @@ -85,16 +113,14 @@ public function loadStagesFromYaml($path)

/**
* Get the stage that is the first necessary stage
*
* @param $name
* @return StageInterface
*/
public function getCurrentStage($name)
public function getCurrentStage(string $name): StageInterface
{
// compute the stageClass from Request parameter
$stageClass = $this->getStageClassName($name);

// loop each stage until finds the first that is necessary

do {
$useCurrentStage = false;
/** @var StageInterface $currentStage */
Expand All @@ -113,38 +139,31 @@ public function getCurrentStage($name)
} else {
$currentStage = $this->getNextStage();
}
} while ($useCurrentStage == false);
} while (false === $useCurrentStage);

return $currentStage;
}

/**
* Get an instance of the previous stage
*
* @return StageInterface
*/
public function getPreviousStage()
public function getPreviousStage(): StageInterface
{
return $this->getSequentialStage('prev');
}

/**
* Get an instance of the next stage
*
* @return StageInterface
*/
public function getNextStage()
public function getNextStage(): StageInterface
{
return $this->getSequentialStage('next');
}

/**
* get either previous or next stage
*
* @param $direction (prev|next)
* @return StageInterface|null
* Get either previous or next stage
*/
private function getSequentialStage($direction)
private function getSequentialStage(string $direction): ?StageInterface
{
$dir = in_array($direction, ['prev', 'next']) ? $direction : 'next';
ksort($this->stageOrder);
Expand All @@ -154,7 +173,6 @@ private function getSequentialStage($direction)
}
$key = $dir($this->stageOrder);
if (null !== $key && false !== $key) {

return $this->getStageInstance($this->stagesByName[$key]);
}

Expand All @@ -163,61 +181,48 @@ private function getSequentialStage($direction)

/**
* Factory class to instantiate a StageClass
*
* @param $stageClass
* @return StageInterface
*/
private function getStageInstance($stageClass)
private function getStageInstance(string $stageClass): StageInterface
{
if (!class_exists($stageClass)) {
throw new RuntimeException('Error: Could not find requested stage class.');
}
if (in_array("Zikula\\Component\\Wizard\\InjectContainerInterface", class_implements($stageClass))) {

if (in_array("Zikula\\Component\\Wizard\\InjectContainerInterface", class_implements($stageClass), true)) {
return new $stageClass($this->container);
} else {

return new $stageClass();
}

return new $stageClass();
}

/**
* Has the wizard been halted?
*
* @return bool
*/
public function isHalted()
public function isHalted(): bool
{
return (!empty($this->warning));
return !empty($this->warning);
}

/**
* get any warning currently set
*
* @return string
* Get any warning currently set
*/
public function getWarning()
public function getWarning(): string
{
return "WARNING: The Wizard was halted for the following reason. This must be corrected before you can continue. " . $this->warning;
return 'WARNING: The Wizard was halted for the following reason. This must be corrected before you can continue. ' . $this->warning;
}

/**
* Match the stage and return the stage classname or default.
*
* @param $name
* @return string
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
private function getStageClassName($name)
private function getStageClassName(string $name): string
{
if (!empty($this->stagesByName[$name])) {

return $this->stagesByName[$name];
}
if (!empty($this->defaultStage) && !empty($this->stagesByName[$this->defaultStage])) {

return $this->stagesByName[$this->defaultStage];
}
throw new \InvalidArgumentException('The request stage could not be found and there is no default stage defined.');
throw new InvalidArgumentException('The request stage could not be found and there is no default stage defined.');
}
}
11 changes: 6 additions & 5 deletions WizardCompleteInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Copyright Zikula Foundation 2014.
*
Expand All @@ -16,14 +19,12 @@
namespace Zikula\Component\Wizard;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

interface WizardCompleteInterface
{
/**
* Get the Response (probably RedirectResponse) for this completed Wizard
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response;
* Get the Response (probably RedirectResponse) for this completed Wizard.
*/
public function getResponse(Request $request);
public function getResponse(Request $request): Response;
}
Loading

0 comments on commit 1d19a5c

Please sign in to comment.