nc-vue-agency/lib/Service/AgencyService.php

44 lines
978 B
PHP

<?php
namespace OCA\Agency\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\Agency\Db\Agency;
use OCA\Agency\Db\AgencyMapper;
class AgencyService {
private $mapper;
public function __construct(AgencyMapper $mapper){
$this->mapper = $mapper;
}
private function handleException ($e) {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($e->getMessage());
} else {
throw $e;
}
}
public function find(int $id) {
try {
return $this->mapper->find($id);
} catch(Exception $e) {
$this->handleException($e);
}
}
public function create(string $name) {
$agency = new Agency();
$agency->setName($name);
return $this->mapper->insert($agency);
}
}