Skip to content

Commit

Permalink
Decoupled the path of default.png
Browse files Browse the repository at this point in the history
  • Loading branch information
benatespina committed Sep 8, 2015
1 parent 18dca16 commit 9dd05b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions Bundle/UserBundle/Resources/config/factories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ services:
- %kreta_user.user.class%
- @kreta_media.factory.media
- @kreta_user.uploader.image_user
- '../../../Bundle/UserBundle/Resources/public/img/'
30 changes: 20 additions & 10 deletions Component/User/Factory/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
class UserFactory
{
const DEFAULT_PHOTO_PATH = '/../../../Bundle/UserBundle/Resources/public/img/';
const DEFAULT_PHOTO_FILENAME = 'default.png';

/**
Expand All @@ -39,6 +38,13 @@ class UserFactory
*/
protected $mediaFactory;

/**
* The path.
*
* @var string|null
*/
protected $path;

/**
* The uploader.
*
Expand All @@ -52,11 +58,13 @@ class UserFactory
* @param string $className The class name
* @param \Kreta\Component\Media\Factory\MediaFactory $mediaFactory The media factory
* @param \Kreta\Component\Media\Uploader\Interfaces\MediaUploaderInterface $uploader The uploader
* @param string|null $path The path, by default null
*/
public function __construct($className, MediaFactory $mediaFactory, MediaUploaderInterface $uploader)
public function __construct($className, MediaFactory $mediaFactory, MediaUploaderInterface $uploader, $path = null)
{
$this->className = $className;
$this->mediaFactory = $mediaFactory;
$this->path = $path;
$this->uploader = $uploader;
}

Expand All @@ -79,19 +87,21 @@ public function create($email, $username, $firstName, $lastName, $enabled = fals
$user->setConfirmationToken(sprintf('%s%s', uniqid('kreta'), unixtojd()));
}

$photo = $this->mediaFactory->create(
new UploadedFile(
__DIR__ . self::DEFAULT_PHOTO_PATH . self::DEFAULT_PHOTO_FILENAME, self::DEFAULT_PHOTO_FILENAME
)
);
$this->uploader->upload($photo);
if (null !== $this->path) {
$photo = $this->mediaFactory->create(
new UploadedFile(
__DIR__ . '/' . $this->path . self::DEFAULT_PHOTO_FILENAME, self::DEFAULT_PHOTO_FILENAME
)
);
$this->uploader->upload($photo);
$user->setPhoto($photo);
}

return $user
->setUsername($username)
->setFirstName($firstName)
->setLastName($lastName)
->setEmail($email)
->setEnabled($enabled)
->setPhoto($photo);
->setEnabled($enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace spec\Kreta\Component\User\Factory;

use Kreta\Component\Media\Factory\MediaFactory;
use Kreta\Component\Media\Model\Interfaces\MediaInterface;
use Kreta\Component\Media\Uploader\Interfaces\MediaUploaderInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
Expand All @@ -34,12 +33,8 @@ function it_is_initializable()
$this->shouldHaveType('Kreta\Component\User\Factory\UserFactory');
}

function it_creates_a_user(MediaFactory $mediaFactory, MediaInterface $photo, MediaUploaderInterface $uploader)
function it_creates_a_user()
{
$mediaFactory->create(Argument::type('Symfony\Component\HttpFoundation\File\UploadedFile'))
->shouldBeCalled()->willReturn($photo);
$uploader->upload($photo)->shouldBeCalled();

$this->create('kreta@kreta.com', 'user', 'First Name', 'Last Name', false)
->shouldReturnAnInstanceOf('Kreta\Component\User\Model\Interfaces\UserInterface');
}
Expand Down

0 comments on commit 9dd05b0

Please sign in to comment.