Code: Select all
if (strlen ($_FILES['image']['name']) > 0) {
$image_data = $this->imagesGateway->insert(array ('filename' => ''));
$image_data['filename'] = 'images/image' . $image_data['id'] . '.jpg';
$this->imagesGateway->update ($image_data['id'], $image_data);
$image = $this->factory->create ($_FILES['image']);
$image->saveJpeg ('../' . $image_data['filename']);
$_POST['image_id'] = $image_data['id'];
} elseif ($_POST['remove_image']) {
$this->imagesGateway->delete ($page->image_id);
@unlink ('../images/image' . $page->image_id . '.jpg');
}
$this->pagesGateway->update ($page->id, $_POST);I feel like this is way too much for the controller to handle. I would love it if I could refactor out some code into something like this:
Code: Select all
if ($this->files->get ('image')) {
try {
$image = $this->imagesGateway->save ($this->files->get ('image'));
} catch (Exception $e) {
$this->addError ($e->getMessage());
}
$page->add ($image);
$pagesGatway->update ($page->id, $page)
}And then, I've got two separate places I need to save that image: physically in the filesystem, preferably with a name that links it to a record in the database ('images/image60.jpg') and then I need to save in the database, preferably with the whole url. I could make two separate gateways, but as you can see these overlap. I could make one gateway... but I worry that I won't have enough control at that point. What if I need to save an image in another location? What if I need images saved in a different part of the database?
In short, I feel like there are things I can be doing to make this controller much lighter, but I'm not really sure what those things are...