Created
August 14, 2015 22:34
-
-
Save anonymous/b403f6a3fb651b658d7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Devgol\DBGenerator; | |
use App\Devgol\DBManager\DBVehicle; | |
use App\Devgol\Locator\LocationValidator; | |
use App\Image; | |
use App\Location; | |
use App\Postcode; | |
use App\User; | |
use App\Vehicle; | |
use App\Video; | |
use DB; | |
use Devgol\Json\Exceptions\IAmABugException; | |
use Faker\Factory; | |
/** | |
* Generate a dummy database for the website. | |
* | |
* Class DBGenerator | |
* @package App\Devgol | |
*/ | |
class DBGenerator | |
{ | |
protected $faker; | |
protected $location_is_set; | |
protected $locations_ids; | |
/** | |
* DBGenerator constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->faker = Factory::create(); | |
$this->locations_ids = Location::get(['id'])->pluck('id'); | |
$this->cleanup(); | |
if (Location::count()) { | |
$this->users(); | |
$this->vehicles(); | |
$this->shine(); | |
} else { | |
$this->locations(); | |
} | |
} | |
/** | |
* Clean up | |
*/ | |
public function cleanup() | |
{ | |
$this->location_is_set = false; | |
DB::table('videos')->delete(); | |
DB::table('social_media')->delete(); | |
DB::table('vehicles')->delete(); | |
DB::table('cars')->delete(); | |
DB::table('bikes')->delete(); | |
DB::table('users')->delete(); | |
DB::table('images')->delete(); | |
} | |
/** | |
* Just, shine. | |
*/ | |
public function shine() | |
{ | |
$users = User::all(); | |
$faker = $this->faker; | |
foreach ($users as $user) { | |
// FAVORITES | |
$nb = $faker->numberBetween(0, 100); | |
for ($i = 0; $i < $nb; $i ++) { | |
$vehicle = Vehicle::orderByRaw('RAND()')->first(); | |
if ($user->isFavoring($vehicle)) { | |
continue; | |
} | |
$user->favor($vehicle); | |
} | |
// WISHS | |
$nb = $faker->numberBetween(0, 10); | |
for ($i = 0; $i < $nb; $i ++) { | |
$vehicle = Vehicle::orderByRaw('RAND()')->first(); | |
if ($user->isWishing($vehicle)) { | |
continue; | |
} | |
$user->wish($vehicle); | |
} | |
// OFFERS | |
$nb = $faker->numberBetween(0, 20); | |
for ($i = 0; $i < $nb; $i ++) { | |
$vehicle = Vehicle::orderByRaw('RAND()')->first(); | |
if ($user->isOfferSentTo($vehicle)) { | |
continue; | |
} | |
$user->sendOffer($vehicle); | |
} | |
} | |
} | |
/** | |
* Generate the locations table. | |
*/ | |
public function locations() | |
{ | |
throw new \Exception( | |
'please run: mysql -p -uroot -D motortrader < app/Devgol/DBGenerator/assets/locations.sql' | |
); | |
//$locations = file(__DIR__ . '/assets/cities'); | |
//try { | |
// foreach ($locations as $location) { | |
// if (Location::where('user_location', clean(trim($location)))->first()) { | |
// continue; | |
// } | |
// new LocationValidator('', [trim($location)]); | |
// } | |
//} catch (\Exception $e) { | |
// sleep(2); | |
// $this->locations(); | |
//} | |
} | |
/** | |
* Generate the users. | |
*/ | |
public function users() | |
{ | |
$faker = $this->faker; | |
$nb_users = iterator_count( | |
new \FilesystemIterator( | |
__DIR__ . '/assets/users', | |
\FilesystemIterator::SKIP_DOTS | |
) | |
); | |
for ($i = 0; $i < 5; $i ++) { | |
$user = User::create([ | |
'name' => unformat(clean($this->faker->name, ' ')), | |
'password' => bcrypt('t@righ'), | |
'email' => $this->faker->email, | |
'location_id' => $this->getLocationId(), | |
'postcode_id' => $this->getPostcodeId(), | |
'picture' => $this->getUserPicture($i), | |
]); | |
if ($this->faker->boolean) { | |
$user->verified = true; | |
$user->token = null; | |
$user->save(); | |
} | |
if ($faker->boolean) { | |
$user->socialMedia->public = $faker->boolean(70); | |
$user->socialMedia->facebook = $faker->boolean ? 'http://facebook.com/' . str_random(10) : ''; | |
$user->socialMedia->twitter = $faker->boolean ? 'http://twitter.com/' . str_random(10) : ''; | |
$user->socialMedia->skype = $faker->boolean ? $faker->username : ''; | |
$user->socialMedia->email = $faker->boolean ? $faker->email : ''; | |
$user->socialMedia->phone = $faker->boolean ? $faker->phoneNumber : ''; | |
$user->socialMedia->save(); | |
} | |
$this->location_is_set = false; | |
} | |
} | |
/** | |
* Generate the vehicles | |
*/ | |
public function vehicles() | |
{ | |
$faker = $this->faker; | |
for ($i = 0; $i < 50; $i++) { | |
$user = User::orderByRaw('RAND()')->first(); | |
auth()->login($user); | |
if ($this->faker->boolean) { | |
$vehicle = Vehicle::create([ | |
'user_id' => $user->id, | |
'type' => 'car', | |
]); | |
} else { | |
$vehicle = Vehicle::create([ | |
'user_id' => $user->id, | |
'type' => 'bike', | |
]); | |
} | |
$limits = $vehicle->limits; | |
$limits->nb_features = $this->faker->numberBetween(3, 10); | |
$limits->nb_images = $this->faker->numberBetween(2, 10); | |
$limits->save(); | |
$vehicle->location_id = $this->getLocationId(); | |
$vehicle->postcode_id = $this->getPostcodeId(); | |
$vehicle->reg = $faker->boolean ? str_random($this->faker->numberBetween(2, 3)) : ""; | |
$vehicle->first_registration_date = $faker->boolean ? $this->faker->date : "0000-00-00"; | |
$vehicle->status = $faker->boolean ? [ | |
DBVehicle::STATUS_NEW, | |
DBVehicle::STATUS_OLD, | |
DBVehicle::STATUS_NEARLY_NEW | |
][$this->faker->numberBetween(0, 2)] : ""; | |
$vehicle->nb_owner = $faker->boolean ? $this->faker->numberBetween(1, 10) : "0"; | |
$vehicle->detail = $faker->boolean ? $this->faker->sentence : ""; | |
$vehicle->make = $this->faker->word; | |
$vehicle->model = join(' ', $this->faker->words($this->faker->numberBetween(2, 5))); | |
$vehicle->price = $faker->boolean ? $faker->numberBetween(100, 2000) : $faker->numberBetween(2000, 100000); | |
$vehicle->age = $faker->boolean ? $this->faker->numberBetween(1, 10) : "0"; | |
$vehicle->mileage = $faker->boolean ? | |
$faker->boolean ? $faker->numberBetween(1, 1000) : $faker->numberBetween(1, 99999) | |
: "0"; | |
$vehicle->color = $faker->boolean ? $this->faker->colorName : ""; | |
$vehicle->trade_type = [ | |
DBVehicle::TRADE_PRIVATE, | |
DBVehicle::TRADE_SELLER, | |
][$this->faker->numberBetween(0, 1)]; | |
if ($faker->boolean(80)) { | |
$this->setVehicleImages($vehicle); | |
} | |
if ($faker->boolean(60)) { | |
$this->setVehicleVideo($vehicle); | |
} | |
$vehicle->save(); | |
$this->completeVehicleInfo($vehicle); | |
$this->location_is_set = false; | |
} | |
} | |
/** | |
* @param $vehicle | |
* @throws IAmABugException | |
*/ | |
public function completeVehicleInfo($vehicle) | |
{ | |
if ($vehicle->type === 'car') { | |
$this->buildCar($vehicle->getVehicle(), $vehicle->limits->nb_features); | |
} elseif ($vehicle->type === 'bike') { | |
$this->buildBike($vehicle->getVehicle()); | |
} else { | |
throw new IAmABugException( | |
sprintf("'%s' unknown vehicle type!", $vehicle->type) | |
); | |
} | |
} | |
/** | |
* Build the car. | |
* | |
* @param $vehicle | |
*/ | |
public function buildCar($car, $limit) | |
{ | |
$faker = $this->faker; | |
$car->features = $faker->boolean ? $this->getFromAssets('features', $limit) : ""; | |
$car->service_history = $faker->boolean ? $this->getFromAssets('service_history', 1) : ""; | |
$car->mot_due = $faker->boolean ? $this->getFromAssets('mot_due', 1) : ""; | |
$car->body = $faker->boolean ? $this->getFromAssets('body', 1) : ""; | |
$car->fuel_type = $faker->boolean ? $this->getFromAssets('fuel_type', 1) : ''; | |
$car->acceleration = $faker->boolean ? $this->getFromAssets('acceleration', 1) : ''; | |
$car->gearbox = $faker->boolean ? $this->getFromAssets('gearbox', 1) : ''; | |
$car->co2_emission = $faker->boolean ? $this->getFromAssets('co2_emission', 1) : ''; | |
$car->nb_door = $faker->boolean ? $faker->numberBetween(2, 8) : '0'; | |
$car->nb_seat = $faker->boolean ? $faker->numberBetween(2, 10) : '0'; | |
$car->insurance_group = $faker->boolean ? $this->getFromAssets('insurance_group', 1) : ''; | |
$car->annual_tax = $faker->boolean ? $this->getFromAssets('annual_tax', 1) : ''; | |
$car->save(); | |
} | |
public function buildBike($bike) | |
{ | |
$bike->cc = $this->faker->numberBetween(100, 1000); | |
$bike->type = $this->faker->boolean ? $this->getFromAssets('type', 1) : ''; | |
$bike->save(); | |
} | |
/** | |
* @param $limit | |
* @return string | |
*/ | |
public function getFromAssets($res, $limit) | |
{ | |
$raw_items = file(__DIR__ . '/assets/' . $res); | |
$items = []; | |
$nb_req_items = $this->faker->numberBetween(1, $limit); | |
for ($i = 0; $i < $nb_req_items; $i ++) { | |
if ($this->faker->boolean(90)) { | |
$item = $raw_items[mt_rand(0, count($raw_items) - 1)]; | |
if (! in_array($item, $items)) { | |
$items[] = trim($item); | |
} | |
} else { | |
$items[] = $this->faker->sentence; | |
} | |
} | |
return join(';', $items); | |
} | |
/** | |
* Set the vehicle images. | |
* | |
* @param $vehicle | |
*/ | |
public function setVehicleImages($vehicle) | |
{ | |
$ids = []; | |
$assets_path = __DIR__ . '/assets/' . $vehicle->type; | |
$images_path = public_path('/images/vehicles'); | |
$relative_path = '/images/vehicles'; | |
for ($i = 0; $i < mt_rand(2, $vehicle->limits->nb_images); $i ++) { | |
$image = new Image; | |
$splFileInfo = $this->getRandomFilename($assets_path); | |
$raw_filename = $splFileInfo->getFilename(); | |
$filename = cleanFilename($raw_filename, $splFileInfo->getSize()); | |
copy($assets_path . '/' . $raw_filename, $images_path . '/' . $filename); | |
$image->vehicle_id = $vehicle->id; | |
$image->path = $relative_path . '/' . $filename; | |
$image->save(); | |
$ids[] = $image->id; | |
} | |
$rand_id = $ids[$this->faker->numberBetween(0, count($ids) - 1)]; | |
$image = Image::findOrFail($rand_id)->first(); | |
$image->is_default = true; | |
$image->save(); | |
} | |
/** | |
* Set the vehicle video. | |
* | |
* @param $vehicle | |
*/ | |
public function setVehicleVideo($vehicle) | |
{ | |
$assets_path = __DIR__ . '/assets/video'; | |
$relative_path = '/images/vehicles/videos'; | |
$public_path = public_path($relative_path); | |
$splFileInfo = $this->getRandomFilename($assets_path); | |
$raw_filename = $splFileInfo->getFilename(); | |
$filename = cleanFilename($raw_filename, $splFileInfo->getSize()); | |
copy($assets_path . '/' . $raw_filename, $public_path . '/' . $filename); | |
$video = new Video; | |
$video->path = $public_path . '/' . $filename; | |
$video->vehicle_id = $vehicle->id; | |
$video->save(); | |
} | |
/** | |
* Get the user picture. | |
* | |
* @return string | |
* @throws \App\Exceptions\FilenameNotValidException | |
*/ | |
public function getUserPicture($i) | |
{ | |
$assets_path = __DIR__ . '/assets/users'; | |
$pictures_path = public_path('images/users'); | |
$pictures_relative_path = '/images/users'; | |
$it = new \FilesystemIterator($assets_path, \FilesystemIterator::SKIP_DOTS); | |
$it->seek($i); | |
$splFileInfo = $it->valid() ? $it->current() : $this->getRandomFilename($assets_path); | |
$raw_filename = $splFileInfo->getFilename(); | |
$filename = cleanFilename($splFileInfo->getFilename(), $splFileInfo->getSize()); | |
copy($assets_path . '/' . $raw_filename, $pictures_path . '/' . $filename); | |
return $pictures_relative_path . '/' .$filename; | |
} | |
/** | |
* Get a random filename from a given directory. | |
* | |
* @param $dir | |
* @return mixed | |
*/ | |
public function getRandomFilename($dir) | |
{ | |
$it = new \FilesystemIterator($dir, \FilesystemIterator::SKIP_DOTS); | |
$rand_file = $this->faker->numberBetween(1, iterator_count($it)); | |
$it->seek($rand_file); | |
if ($it->valid()) { | |
return $it->current(); | |
} else { | |
return $this->getRandomFilename($dir); | |
} | |
} | |
/** | |
* Get the location id. | |
*/ | |
public function getLocationId() | |
{ | |
$is_location = $this->faker->boolean; | |
if ($is_location) { | |
$rand_id = $this->locations_ids->random(); | |
$location = Location::findOrFail($rand_id)->first()->user_location; | |
new LocationValidator('', [$location]); | |
} else { | |
$this->getPostcodeId(); | |
return null; | |
} | |
$this->location_is_set = true; | |
return session('location.id'); | |
} | |
/** | |
* Get the postcode id. | |
*/ | |
public function getPostcodeId() | |
{ | |
if ($this->location_is_set) { | |
return null; | |
} | |
$rand_id = $this->faker->numberBetween(1, Postcode::count()); | |
$postcode = Postcode::find($rand_id)->postcode; | |
new LocationValidator(null, [null, null, null, null, $postcode]); | |
return session('location.id'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment