/**
  * @param array $data
  * @param int $id
  * @return bool
  * @throws \Exception
  */
 protected function save(array $data, $id = null)
 {
     $this->form->setData($data);
     if (!$this->form->isValid()) {
         throw new \Exception('Form failed to validate. Unable to save album');
     }
     /**
      * @var Album $album
      */
     $album = $this->form->getData();
     $this->repository->save($album, $id);
     return true;
 }
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         /**
          * @var Session $session
          */
         $session = $request->getAttribute('session');
         $album = new Album();
         $this->form->bind($album);
         $this->form->get('submit')->setValue('Add');
         if ($request->getMethod() === 'POST') {
             $this->albumService->addAlbum($request->getParsedBody());
             $session->getSegment('App\\Album')->setFlash('flash', ['type' => 'success', 'message' => sprintf('Successfully added album %s (%s)', $album->getTitle(), $album->getArtist())]);
             return new RedirectResponse($this->router->generateUri('album.index'));
         }
     } catch (\Exception $e) {
         // perhaps log an error and display a message to the user
     }
     return new HtmlResponse($this->template->render('album::add', ['form' => $this->form]));
 }