vendor/troopers/alertify-bundle/Troopers/AlertifyBundle/Handler/AlertifySessionHandler.php line 44

Open in your IDE?
  1. <?php
  2. namespace Troopers\AlertifyBundle\Handler;
  3. use Symfony\Component\HttpFoundation\Session\Session;
  4. use Twig\Environment;
  5. /**
  6.  * AlertifySessionHandler.
  7.  */
  8. class AlertifySessionHandler
  9. {
  10.     /**
  11.      * @var Environment
  12.      */
  13.     protected $twig;
  14.     /**
  15.      * @var array
  16.      */
  17.     private $defaultParameters;
  18.     /**
  19.      * AlertifySessionHandler constructor.
  20.      *
  21.      * @param Environment $twig
  22.      * @param array       $defaultParameters
  23.      */
  24.     public function __construct(array $defaultParameters)
  25.     {
  26.         $this->defaultParameters $defaultParameters;
  27.     }
  28.     /**
  29.      * Alertify.
  30.      *
  31.      * @param Environment $twig
  32.      * @param Session     $session
  33.      *
  34.      * @return string
  35.      */
  36.     public function handle($sessionEnvironment $twig)
  37.     {
  38.         $flashes $session->getFlashBag()->all();
  39.         $renders = [];
  40.         foreach ($flashes as $type => $flash) {
  41.             foreach ($flash as $key => $content) {
  42.                 if (is_array($content)) {
  43.                     $context = isset($content['context']) ? $content['context'] : null;
  44.                     $defaultParameters self::getDefaultParametersFromContext($context);
  45.                     $parameters array_replace_recursive($defaultParameters$content);
  46.                 } else {
  47.                     $defaultParameters self::getDefaultParametersFromContext(null);
  48.                     $parameters array_replace($defaultParameters, ['body' => $content]);
  49.                 }
  50.                 $parameters['type'] = $type;
  51.                 $renders[$type.$key] = $twig->render('@TroopersAlertify/'.$parameters['engine'].'.html.twig'$parameters);
  52.             }
  53.         }
  54.         return implode(' '$renders);
  55.     }
  56.     /**
  57.      * Get the configuration for the given context.
  58.      *
  59.      * @param string $context The actual context
  60.      *
  61.      * @return array
  62.      **/
  63.     protected function getDefaultParametersFromContext($context null)
  64.     {
  65.         if (count($this->defaultParameters['contexts'])) {
  66.             //If context is not given, just take the default one
  67.             if ($context === null) {
  68.                 $context $this->defaultParameters['default']['context'];
  69.             }
  70.             //If context is in declared contexts, we use it
  71.             if (array_key_exists($context$this->defaultParameters['contexts'])) {
  72.                 return $this->defaultParameters['contexts'][$context];
  73.             }
  74.         }
  75.         //else we return the default configuration
  76.         return $this->defaultParameters['default'];
  77.     }
  78. }