Zend Framework and the prevention of fat controllers

Avoid Fat Controller

So, I am using the Zend Framework, and I have a question related to preventing thick controllers from being one of my actions. Basically, I normalize the CSV file to my database.

This means that I need to get a feed and then use my model. Capturing feed is just to show how it works, but now it's an action helper.

I am using the Data Mapper template with Zend Framework. I hate that I do this in my controller. All these setProperty () -> setProperty () -> setProperty () look incredibly ugly, and I feel like I'm doing it in the wrong place? Would it be better to just create some kind of service level where I pass all the $ feed , and then in this class I create my models and my Mapper?

Also, I need to normalize, which means I have to use a transaction, but I'm not sure where I should start the transaction. Due to the way I am currently doing, the only place I could consider is in my controller. wow .. that would be a terrible place.

How can I get model behavior and operations from my controller?

ImportController.php

public function indexAction() {
        $start = $this->getRequest()->getParam('start');
        $end = $this->getRequest()->getParam('end');
        $url = "http://www.domain.com/admin/GetBookingData.aspx";       

        $client = new Zend_Http_Client();
        $client->setParameterGet('dateEnteredMin', $start);
        $client->setParameterGet('dateEnteredMax', $end);
        $client->setParameterGet('login', 'login');
        $client->setParameterGet('password', 'password');
        $client->setUri( $url );
        $client->setConfig(array(
            'maxredirects' => 0,
            'timeout'      => 30));
        // Send the request. 
        $response = $client->request();

        // Grab the feed from ->getBody and add it to $feed
        $feed = $this->csv_to_array(trim($response->getBody()));


        // The first item in the array is the heading in the CSV, so we can remove it from the array using shift().
        $title = array_shift($feed);

        // Create my Models and Mappers.
            // ***  EVERYTHING BELOW HERE IS WHAT I DON'T LIKE ***
        $bookings =         new Bookings_Models_Bookings();
        $property =         new Bookings_Models_Property();
        $clients =      new Bookings_Models_Clients();

        $bookingsMapper =   new Bookings_Models_Bookings_Mapper();
        $propertyMapper =   new Bookings_Models_Property_Mapper();
        $clientsMapper =    new Bookings_Models_Clients_Mapper();

        $bookings->setId($feed[9])
            ->setPropertyId($feed[1])
            ->setClientId($feed[2])
            ->setDate($feed[4]);
        $bookingsMapper->save($bookings);

        $property->setId($feed[1])
            ->setPropertyName($feed[23])
        $propertyMapper->save($bookings);

        $clients->setId($feed[2])
            ->setFirstName($feed[20])
            ->setLastName($feed[21])
        $clientsMapper->save($clients);

}
+3
2

, , . , , :

class Your_Service_Import
{
    public function importFromCsv($csv)
    {
        // etc.
    }
}

, csv_to_array :

$feed = $this->csv_to_array(trim($response->getBody()));

$service = new Your_Service_Import();
$service->importFromCsv($feed);

( ) .

+3

( ) , @Tim Fountain

  • , , ( URL-) csv .
  • , (csv) .

$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$dataService = new Your_Service_Get();
$data = $dataService->get($start, $end);
$mapService = new Your_Service_Map();
$mapService->map($data);
+1

Source: https://habr.com/ru/post/1756990/


All Articles