Skip to content

Commit

Permalink
Merge pull request #143 from GrahamCampbell/tweaks
Browse files Browse the repository at this point in the history
Tweaks
  • Loading branch information
scottrobertson committed Jul 29, 2014
2 parents c96d9dd + c92b30d commit 21c0d11
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 88 deletions.
12 changes: 12 additions & 0 deletions src/Facade/FactoryMuffin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
*
* @see League\FactoryMuffin\FactoryMuffin
*
* @method static void setSaveMethod(string $method) Set the method we use when saving objects.
* @method static void setDeleteMethod(string $method) Set the method we use when deleting objects.
* @method static object create(string $model, array $attr) Creates and saves in db an instance of the model.
* @method static mixed save(object $object) Save our object to the db, and keep track of it.
* @method static object[] saved() Return an array of saved objects.
* @method static void deleteSaved() Call the delete method on any saved objects.
* @method static object instance(string $model, array $attr, bool $save) Return an instance of the model.
* @method static array attributesFor(string $model, array $attr, bool $save) Returns the mock attributes for the model.
* @method static void define(string $model, array $definition) Define a new model factory.
* @method static string|object generateAttr(string $kind, string $model, bool $save) Generate the attributes.
* @method static void loadFactories(string|string[] $paths) Load the specified factories.
*
* @package League\FactoryMuffin\Facades
* @author Zizaco <[email protected]>
* @author Scott Robertson <[email protected]>
Expand Down
187 changes: 99 additions & 88 deletions src/FactoryMuffin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class FactoryMuffin
*/
private $saved = array();

/**
* This is the method used when saving objects.
*
* @type string
*/
private $saveMethod = 'save';

/**
* This is the method used when deleting objects.
*
Expand All @@ -46,14 +53,33 @@ class FactoryMuffin
private $deleteMethod = 'delete';

/**
* This is the method used when saving objects.
* Set the method we use when saving objects.
*
* @type string
* @param string $method
*
* @return void
*/
private $saveMethod = 'save';
public function setSaveMethod($method)
{
$this->saveMethod = $method;
}

/**
* Creates and saves in db an instance of Model with mock attributes.
* Set the method we use when deleting objects.
*
* @param string $method
*
* @return void
*/
public function setDeleteMethod($method)
{
$this->deleteMethod = $method;
}

/**
* Creates and saves in db an instance of the model.
*
* This object will be generated with mock attributes.
*
* @param string $model Model class name.
* @param array $attr Model attributes.
Expand All @@ -78,7 +104,7 @@ public function create($model, $attr = array())
}

/**
* Save our object to the DB, and keep track of it.
* Save our object to the db, and keep track of it.
*
* @param object $object
*
Expand All @@ -101,11 +127,55 @@ public function save($object)
}

/**
* Return an instance of the model, which is not saved in the database.
* Return an array of saved objects.
*
* @return object[]
*/
public function saved()
{
return $this->saved;
}

/**
* Call the delete method on any saved objects.
*
* @throws \League\FactoryMuffin\Exception\DeletingFailed
* @throws \League\FactoryMuffin\Exception\DeleteMethodNotFound
*
* @return void
*/
public function deleteSaved()
{
$exceptions = array();
$method = $this->deleteMethod;
foreach ($this->saved() as $saved) {
try {
if (!method_exists($saved, $method)) {
throw new DeleteMethodNotFound($saved, $method);
}

$saved->$method();
} catch (Exception $e) {
$exceptions[] = $e;
}
}

$this->saved = array();

if ($exceptions) {
throw new DeletingFailed($exceptions);
}
}

/**
* Return an instance of the model.
*
* This does not save it in the database. Use the create method to create
* and save to the db, or pass the object from this method into the save method.
*
* @param string $model Model class name.
* @param bool $save Are we saving an object, or just creating an instance?
* @param array $attr Model attributes.
* @param bool $save Are we saving an object, or just creating an instance?
*
* @return object
*/
Expand All @@ -125,7 +195,7 @@ public function instance($model, $attr = array(), $save = false)
}

/**
* Returns an array of mock attributes for the specified model.
* Returns the mock attributes for the model.
*
* @param string $model Model class name.
* @param array $attr Model attributes.
Expand All @@ -147,27 +217,14 @@ public function attributesFor($model, $attr = array(), $save = false)
return $attr;
}

/**
* Define a new model factory.
*
* @param string $model Model class name.
* @param array $definition Array with definition of attributes.
*
* @return void
*/
public function define($model, array $definition = array())
{
$this->factories[$model] = $definition;
}

/**
* Get factory attributes.
*
* @param string $model Model class name.
*
* @throws \League\FactoryMuffin\Exception\NoDefinedFactory
*
* @return mixed
* @return array
*/
private function getFactoryAttrs($model)
{
Expand All @@ -179,7 +236,22 @@ private function getFactoryAttrs($model)
}

/**
* Generate the attributes and return a string, or an instance of the model.
* Define a new model factory.
*
* @param string $model Model class name.
* @param array $definition Array with definition of attributes.
*
* @return void
*/
public function define($model, array $definition = array())
{
$this->factories[$model] = $definition;
}

/**
* Generate the attributes.
*
* This method will return a string, or an instance of the model.
*
* @param string $kind The kind of attribute that will be generate.
* @param string $model The name of the model class.
Expand All @@ -197,6 +269,10 @@ public function generateAttr($kind, $model = null, $save = false)
/**
* Load the specified factories.
*
* This method expects either a single path to a directory containing php
* files, or an array of directory paths, and will include_once every file.
* These files should contain factory definitions for your models.
*
* @param string|string[] $paths
*
* @throws \League\FactoryMuffin\Exception\DirectoryNotFound
Expand All @@ -214,71 +290,6 @@ public function loadFactories($paths)
}
}

/**
* Return an array of saved objects.
*
* @return object[]
*/
public function saved()
{
return $this->saved;
}

/**
* Call the delete method on any saved objects.
*
* @throws \League\FactoryMuffin\Exception\DeletingFailed
* @throws \League\FactoryMuffin\Exception\DeleteMethodNotFound
*
* @return void
*/
public function deleteSaved()
{
$exceptions = array();
$method = $this->deleteMethod;
foreach ($this->saved() as $saved) {
try {
if (!method_exists($saved, $method)) {
throw new DeleteMethodNotFound($saved, $method);
}

$saved->$method();
} catch (Exception $e) {
$exceptions[] = $e;
}
}

$this->saved = array();

if ($exceptions) {
throw new DeletingFailed($exceptions);
}
}

/**
* Set the method we use when deleting objects.
*
* @param string $method
*
* @return void
*/
public function setDeleteMethod($method)
{
$this->deleteMethod = $method;
}

/**
* Set the method we use when saving objects.
*
* @param string $method
*
* @return void
*/
public function setSaveMethod($method)
{
$this->saveMethod = $method;
}

/**
* Load all the files in a directory.
*
Expand Down

0 comments on commit 21c0d11

Please sign in to comment.