Page MenuHomeCode

PersoSelector.php
No OneTemporary

PersoSelector.php

<?php
namespace Zed\Engines\Perso;
use Keruald\Database\DatabaseEngine;
use LogicException;
use Smarty;
use Zed\Engines\Perso\Events\BaseEvent;
use Zed\Engines\Perso\Events\Create;
use Zed\Engines\Perso\Events\Logout;
use Zed\Engines\Perso\Events\ReadFromSession;
use Zed\Engines\Perso\Events\Select;
use Zed\Engines\Perso\Events\TryAutoSelect;
use Zed\Models\Objects\Perso;
use Zed\Models\Objects\User;
class PersoSelector {
///
/// Properties
///
public DatabaseEngine $db;
public User $user;
public Smarty $smarty;
public Perso $perso;
public bool $hasPerso = false;
///
/// Constructors
//
/**
* @param User $user The currently logged user
*/
public function __construct (DatabaseEngine $db, User $user, Smarty $smarty) {
$this->db = $db;
$this->smarty = $smarty;
$this->user = $user;
}
///
/// Events processing
///
/**
* Select a perso using several strategies in a sequential order.
*
* Order matters: for example, to be able to log a perso out,
* we first need to select it from the session.
*
* @return BaseEvent[]
*/
private function getDefaultEvents () : array {
return [
// Strategy 1. Look in session if the perso is already selected.
new ReadFromSession($this),
// Strategy 2. Process forms and actions from URL.
new Create($this),
new Logout($this),
new Select($this),
// Strategy 3. Try to autoselect a perso or ask user for one.
new TryAutoSelect($this),
];
}
private function handleEvents () : void {
$events = $this->getDefaultEvents();
foreach ($events as $event) {
$event->run();
}
}
/**
* Run all the workflow to get a perso.
*/
public static function load (DatabaseEngine $db, User $user, Smarty $smarty) : Perso {
$selector = new self($db, $user, $smarty);
$selector->handleEvents();
if (!$selector->hasPerso) {
throw new LogicException(<<<'EOD'
The selector has processed the different events and scenarii
to pick a perso. The expectation after all events have been
handled is we've selected a perso or have printed any view
to create or select one.
As such, this code should be unreachable. Debug the different
'isTriggered' and 'handle' methods of the events to ensure the
last event exit or return a perso.
EOD);
}
$smarty->assign('CurrentPerso', $selector->perso);
return $selector->perso;
}
///
/// Properties
///
/**
* Select a perso, as a fresh login.
*
* If the perso is already logged in,
* we can call PersoSelector::setPerso instead.
*/
public function selectAndSetPerso (Perso $perso) : void {
$perso->on_select();
$this->setPerso($perso);
}
public function setPerso (Perso $perso) : void {
$this->perso = $perso;
$this->hasPerso = true;
}
}

File Metadata

Mime Type
text/x-php
Expires
Sat, Nov 16, 14:26 (1 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
21425
Default Alt Text
PersoSelector.php (3 KB)

Event Timeline