*/ class mpWebController { /** * The used view object. * * @type mpWebView * @var mpWebView $view */ private $view = null; /** * The controller ctor. */ public function __construct() { $this->view = new mpWebView(); $this->view->connect( "clicked", array( $this, "doAddProduct" ) ); } /** * Starts the controller process. */ public function run() { $this->view->show(); } /** * Add product action with the hole domain logic. */ public function doAddProduct() { $name = $this->view->getText(); // Create a new product $product = new mpProduct( $name ); // Get the product catalog $catalog = mpProductCatalog::getInstance(); // Create a new transaction $transaction = $catalog->createTransaction( $product ); try { // Start transaction $transaction->begin(); // Add new product to catalog $catalog->addProduct( $product ); // Commit product transaction $transaction->commit(); // Reset input $this->view->setText( "" ); } catch ( mpProductInTransactionException $e ) { // Show error message $this->showDialog( "Transaction Error", $e->getMessage() ); } catch ( mpProductExistsException $e ) { try { // Rollback product transaction $transaction->rollback(); // Show error message $this->showDialog( "Name Error", $e->getMessage() ); } catch ( mpProductTransactionNotOpenException $e ) { // Show error message $this->showDialog( "Transaction Error", $e->getMessage() ); } } } /** * Shows a simple error dialog. * * @param string $title * @param string $message */ private function showDialog( $title, $message ) { printf( ' ', $title, $message ); } } $controller = new mpWebController(); $controller->run();