Favor object creation in factories (#459)

Instead of passing factories in the constructors and let objects call the factory method, create all dependencies directly in the factories.
This commit is contained in:
Adrien Loison authored and GitHub committed 2017-08-27 02:40:39 +02:00
1 parent 4ec3a21170
commit 61f2addefa
13 files changed
+84 -114

No files matched your search

+24 -22
View File
@@ -3,6 +3,7 @@
namespace Box\Spout\Reader\XLSX\Creator;
use Box\Spout\Reader\Common\Creator\EntityFactoryInterface;
use Box\Spout\Reader\Common\Entity\Options;
use Box\Spout\Reader\Common\XMLProcessor;
use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper;
use Box\Spout\Reader\XLSX\RowIterator;
@@ -33,19 +34,12 @@ class EntityFactory implements EntityFactoryInterface
* @param string $filePath Path of the file to be read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @return SheetIterator
*/
public function createSheetIterator($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper)
public function createSheetIterator($filePath, $optionsManager, $sharedStringsHelper)
{
return new SheetIterator(
$filePath,
$optionsManager,
$sharedStringsHelper,
$globalFunctionsHelper,
$this,
$this->helperFactory
);
$sheetHelper = $this->helperFactory->createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $this);
return new SheetIterator($sheetHelper);
}
/**
@@ -67,16 +61,8 @@ class EntityFactory implements EntityFactoryInterface
$optionsManager,
$sharedStringsHelper)
{
return new Sheet(
$filePath,
$sheetDataXMLFilePath,
$sheetIndex,
$sheetName,
$isSheetActive,
$optionsManager,
$sharedStringsHelper,
$this
);
$rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper);
return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive);
}
/**
@@ -86,9 +72,25 @@ class EntityFactory implements EntityFactoryInterface
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @return RowIterator
*/
public function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper)
private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper)
{
return new RowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper, $this, $this->helperFactory);
$xmlReader = $this->createXMLReader();
$xmlProcessor = $this->createXMLProcessor($xmlReader);
$styleHelper = $this->helperFactory->createStyleHelper($filePath, $this);
$shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES);
$cellValueFormatter = $this->helperFactory->createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates);
$shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
return new RowIterator(
$filePath,
$sheetDataXMLFilePath,
$shouldPreserveEmptyRows,
$xmlReader,
$xmlProcessor,
$cellValueFormatter
);
}
/**
@@ -44,14 +44,13 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory
* @param string $filePath Path of the XLSX file being read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param EntityFactory $entityFactory Factory to create entities
* @return SheetHelper
*/
public function createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $entityFactory)
public function createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $entityFactory)
{
$escaper = $this->createStringsEscaper();
return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $escaper, $entityFactory);
return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $escaper, $entityFactory);
}
/**
+1 -3
View File
@@ -53,16 +53,14 @@ class SheetHelper
* @param string $filePath Path of the XLSX file being read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data
* @param EntityFactory $entityFactory Factory to create entities
*/
public function __construct($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $escaper, $entityFactory)
public function __construct($filePath, $optionsManager, $sharedStringsHelper, $escaper, $entityFactory)
{
$this->filePath = $filePath;
$this->optionsManager = $optionsManager;
$this->sharedStringsHelper = $sharedStringsHelper;
$this->globalFunctionsHelper = $globalFunctionsHelper;
$this->escaper = $escaper;
$this->entityFactory = $entityFactory;
}
+10 -18
View File
@@ -10,9 +10,8 @@ use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Helper\CellHelper;
use Box\Spout\Reader\XLSX\Helper\CellValueFormatter;
use Box\Spout\Reader\XLSX\Helper\StyleHelper;
use Box\Spout\Reader\Common\XMLProcessor;
use Box\Spout\Reader\XLSX\Helper\CellValueFormatter;
/**
* Class RowIterator
@@ -48,9 +47,6 @@ class RowIterator implements IteratorInterface
/** @var Helper\CellValueFormatter Helper to format cell values */
protected $cellValueFormatter;
/** @var Helper\StyleHelper $styleHelper Helper to work with styles */
protected $styleHelper;
/**
* TODO: This variable can be deleted when row indices get preserved
* @var int Number of read rows
@@ -84,25 +80,21 @@ class RowIterator implements IteratorInterface
/**
* @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param Helper\SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param EntityFactory $entityFactory Factory to create entities
* @param HelperFactory $helperFactory Factory to create helpers
* @param bool $shouldPreserveEmptyRows Whether empty rows should be preserved
* @param XMLReader $xmlReader XML Reader
* @param XMLProcessor $xmlProcessor Helper to process XML files
* @param CellValueFormatter $cellValueFormatter Helper to format cell values
*/
public function __construct($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper, $entityFactory, $helperFactory)
public function __construct($filePath, $sheetDataXMLFilePath, $shouldPreserveEmptyRows, $xmlReader, $xmlProcessor, $cellValueFormatter)
{
$this->filePath = $filePath;
$this->sheetDataXMLFilePath = $this->normalizeSheetDataXMLFilePath($sheetDataXMLFilePath);
$this->xmlReader = $entityFactory->createXMLReader();
$this->styleHelper = $helperFactory->createStyleHelper($filePath, $entityFactory);
$this->cellValueFormatter = $helperFactory->createCellValueFormatter($sharedStringsHelper, $this->styleHelper, $optionsManager->getOption(Options::SHOULD_FORMAT_DATES));
$this->shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
$this->xmlReader = $xmlReader;
$this->cellValueFormatter = $cellValueFormatter;
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows;
// Register all callbacks to process different nodes when reading the XML file
$this->xmlProcessor = $entityFactory->createXMLProcessor($this->xmlReader);
$this->xmlProcessor = $xmlProcessor;
$this->xmlProcessor->registerCallback(self::XML_NODE_DIMENSION, XMLProcessor::NODE_TYPE_START, [$this, 'processDimensionStartingNode']);
$this->xmlProcessor->registerCallback(self::XML_NODE_ROW, XMLProcessor::NODE_TYPE_START, [$this, 'processRowStartingNode']);
$this->xmlProcessor->registerCallback(self::XML_NODE_CELL, XMLProcessor::NODE_TYPE_START, [$this, 'processCellStartingNode']);
+3 -14
View File
@@ -26,25 +26,14 @@ class Sheet implements SheetInterface
protected $isActive;
/**
* @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param RowIterator $rowIterator The corresponding row iterator
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $sheetName Name of the sheet
* @param bool $isSheetActive Whether the sheet was defined as active
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param Helper\SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param EntityFactory $entityFactory Factory to create entities
*/
public function __construct(
$filePath,
$sheetDataXMLFilePath,
$sheetIndex, $sheetName,
$isSheetActive,
$optionsManager,
$sharedStringsHelper,
$entityFactory)
public function __construct($rowIterator, $sheetIndex, $sheetName, $isSheetActive)
{
$this->rowIterator = $entityFactory->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper);
$this->rowIterator = $rowIterator;
$this->index = $sheetIndex;
$this->name = $sheetName;
$this->isActive = $isSheetActive;
+2 -14
View File
@@ -23,24 +23,12 @@ class SheetIterator implements IteratorInterface
protected $currentSheetIndex;
/**
* @param string $filePath Path of the file to be read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper $sharedStringsHelper
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param EntityFactory $entityFactory Factory to create entities
* @param HelperFactory $helperFactory Factory to create helpers
* @param SheetHelper $sheetHelper Helper to work with sheets
* @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
*/
public function __construct(
$filePath,
$optionsManager,
$sharedStringsHelper,
$globalFunctionsHelper,
$entityFactory,
$helperFactory)
public function __construct($sheetHelper)
{
// Fetch all available sheets
$sheetHelper = $helperFactory->createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $entityFactory);
$this->sheets = $sheetHelper->getSheets();
if (count($this->sheets) === 0) {