In my Symfony (v2.7.9) project I have a set of entities which share some view functionality i.e being displayed in a table with some of their values as columns.
Following the separation of concerns principle, I've made a ListTable
class which handles getting the table headers, rows and column values and I'm extending this for entities which need a little bit of extra functionality e.g ProductListTable
class ListTable extends AbstractLibrary {
protected $items = [];
protected $query;
protected $controller;
protected $tableKeys = [
'name',
'status',
];
public function __construct( BaseController $controller ) {
$this->controller = $controller;
$this->query = $controller->request->query;
}
public function getTableKeys() {
return $this->tableKeys;
}
public function getItems() {
if ( empty( $this->items ) ) {
foreach ( $this->controller->getResults() as $entity ) {
$this->items[] = [
'id' => $entity->getId(),
'href' => $this->controller->getUrl( 'edit', [ 'id' => $entity->getId() ] ),
'columns' => $this->getColumns( $entity ),
];
}
}
return $this->items;
}
...
-
class ProductListTable extends ListTable {
protected $tableKeys = array(
'name',
'sku',
'price',
'brand',
'status',
'date_added',
);
...
Then within a controller method I create an instance of the class with:
new ProductListTable( $this );
There's just something I don't like about having to pass in the entire controller instance so I can use some of the methods/services within it. On the other hand, there are quite a few dependencies including some services I'd need to inject through the constructor if I don't inject the controller.
So what I'd like to know is, is it bad practice to inject the controller or is that preferable to injecting 5/6 dependencies as separate function arguments for the constructor? Or does the fact I require these dependencies within the ListTable
class mean I'm going about it the wrong way?