Skip to content

Commit

Permalink
Update to new DealNews coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmoon committed Apr 23, 2020
1 parent 56db30c commit 7facab2
Show file tree
Hide file tree
Showing 26 changed files with 1,152 additions and 598 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"license": "BSD-3-Clause",
"description": "Database Library providing a PDO factory and CRUD operations",
"require-dev": {
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^9.1"
},
"require": {
"php": ">=7.1.0",
"php": "^7.3",
"dealnews/get-config": "^1.0",
"dealnews/data-mapper": "^0.9.0"
"dealnews/data-mapper": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 7 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
</exclude>
</groups>
<filter>
<whitelist>
<directory suffix="Test.php">./src</directory>
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix=".php">./tests</directory>
<directory suffix=".php">./vendor</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="DN_INI_FILE" value="./tests/test.ini"/>
</php>
</phpunit>
</phpunit>
118 changes: 59 additions & 59 deletions src/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace DealNews\DB;

use \DealNews\DB\CRUD;

/**
* Maps an object to a database accesible via PDO
*
Expand All @@ -16,17 +14,17 @@ abstract class AbstractMapper extends \DealNews\DataMapper\AbstractMapper {
/**
* Database configuration name
*/
public const DATABASE_NAME = "";
public const DATABASE_NAME = '';

/**
* Table name
*/
public const TABLE = "";
public const TABLE = '';

/**
* Table primary key column name
*/
public const PRIMARY_KEY = "";
public const PRIMARY_KEY = '';

/**
* Sequence name for DBMS that use sequences
Expand All @@ -36,12 +34,12 @@ abstract class AbstractMapper extends \DealNews\DataMapper\AbstractMapper {
/**
* Value sent to get_column_name to indicate data is being read
*/
public const MODE_READ = "READ";
public const MODE_READ = 'READ';

/**
* Value sent to get_column_name to indicate data is being written
*/
public const MODE_WRITE = "WRITE";
public const MODE_WRITE = 'WRITE';

/**
* Primititve types
Expand All @@ -57,7 +55,7 @@ abstract class AbstractMapper extends \DealNews\DataMapper\AbstractMapper {
/**
* Name of the class the mapper is mapping
*/
public const MAPPED_CLASS = "";
public const MAPPED_CLASS = '';

/**
* Defines the properties that are mapped and any
Expand All @@ -76,13 +74,12 @@ abstract class AbstractMapper extends \DealNews\DataMapper\AbstractMapper {
* @param \DealNews\DB\CRUD|null $crud Optional CRUD object
*/
public function __construct(CRUD $crud = null) {

if ($crud !== null) {
$this->crud = $crud;
} elseif (!empty($this::DATABASE_NAME)) {
$this->crud = new CRUD(\DealNews\DB\Factory::init($this::DATABASE_NAME));
} else {
throw new \LogicException("No database configuration for ".get_class($this));
throw new \LogicException('No database configuration for ' . get_class($this));
}
}

Expand Down Expand Up @@ -110,7 +107,7 @@ public function load($id) {
* @return boolean|array
* @throws \Error
*/
public function load_multi(array $ids) {
public function loadMulti(array $ids) {
return $this->find([$this::PRIMARY_KEY => $ids]);
}

Expand All @@ -129,9 +126,10 @@ public function find(array $filter) {

if (!empty($data)) {
foreach ($data as $row) {
$object = $this->set_data($row);
$object = $this->load_relations($object);
$objects[$this->get_value($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY])] = $object;
$object = $this->setData($row);
$object = $this->loadRelations($object);

$objects[$this->getValue($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY])] = $object;
}
}

Expand All @@ -144,8 +142,7 @@ public function find(array $filter) {
* @throws \Error
*/
public function save($object) {

$data = $this->get_data($object);
$data = $this->getData($object);

if (array_key_exists($this::PRIMARY_KEY, $data)) {
unset($data[$this::PRIMARY_KEY]);
Expand All @@ -158,10 +155,10 @@ public function save($object) {
}

try {
if ($this->get_value($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY]) == 0) {
if ($this->getValue($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY]) == 0) {
$success = $this->crud->create($this::TABLE, $data);
if ($success) {
$this->set_value(
$this->setValue(
$object,
$this::PRIMARY_KEY,
[$this::PRIMARY_KEY => $this->crud->pdo->lastInsertId($this::SEQUENCE_NAME)],
Expand All @@ -173,28 +170,28 @@ public function save($object) {
$this::TABLE,
$data,
[
$this::PRIMARY_KEY => $object->{$this::PRIMARY_KEY}
$this::PRIMARY_KEY => $object->{$this::PRIMARY_KEY},
]
);
}

if ($success) {
$object = $this->saveRelations($object);
$object = $this->load($this->getValue($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY]));
}
} catch (\PDOException $e) {
if (!$already_in_transaction) {
$this->crud->pdo->rollBack();
}
throw $e;
}

$object = $this->save_relations($object);

$object = $this->load($this->get_value($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY]));

if (!$already_in_transaction) {
$this->crud->pdo->commit();
}

if (!$already_in_transaction) {
if ($success) {
$this->crud->pdo->commit();
} else {
$this->crud->pdo->rollBack();
}

} catch (\PDOException $e) {
$this->crud->pdo->rollBack();
throw $e;
}

return $object;
Expand All @@ -209,9 +206,10 @@ public function delete($id) {
$success = $this->crud->delete(
$this::TABLE,
[
$this::PRIMARY_KEY => $id
$this::PRIMARY_KEY => $id,
]
);

return $success;
}

Expand All @@ -220,31 +218,32 @@ public function delete($id) {
* @param object $object Object to which to add the relations
* @return object
*/
protected function load_relations($object) {
protected function loadRelations($object) {
foreach ($this::MAPPING as $property => $mapping) {
if (!empty($mapping["mapper"]) && !empty($mapping["lookup"])) {
if (!empty($mapping['mapper']) && !empty($mapping['lookup'])) {
$objects = [];
$rows = $this->crud->read(
$mapping["lookup"]["table"],
$rows = $this->crud->read(
$mapping['lookup']['table'],
[
$mapping["lookup"]["foreign_column"] => $object->{$this::PRIMARY_KEY}
$mapping['lookup']['foreign_column'] => $object->{$this::PRIMARY_KEY},
]
);

if (!empty($rows)) {
$mapper = new $mapping["mapper"]();
$mapper = new $mapping['mapper']();
foreach ($rows as $row) {
$objects[] = $mapper->load($row[$mapping["lookup"]["mapper_column"]]);
$objects[] = $mapper->load($row[$mapping['lookup']['mapper_column']]);
}
}
$this->set_value(
$this->setValue(
$object,
$property,
[$property => $objects],
$mapping
);
}
}

return $object;
}

Expand All @@ -254,32 +253,32 @@ protected function load_relations($object) {
* @param object $object Object containing the relations
* @return object
*/
protected function save_relations($object) {
protected function saveRelations($object) {
foreach ($this::MAPPING as $property => $mapping) {
if (!empty($mapping["mapper"]) && !empty($mapping["lookup"])) {
if (!empty($mapping['mapper']) && !empty($mapping['lookup'])) {

// load values in the lookup table
$rows = $this->crud->read(
$mapping["lookup"]["table"],
$mapping['lookup']['table'],
[
$mapping["lookup"]["foreign_column"] => $this->get_value($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY])
$mapping['lookup']['foreign_column'] => $this->getValue($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY]),
]
);

// save current object values
$mapper = new $mapping["mapper"]();
$mapper = new $mapping['mapper']();

$objects = $this->get_value($object, $property, $mapping);
$objects = $this->getValue($object, $property, $mapping);

foreach ($objects as $key => $obj) {
$objects[$key] = $mapper->save($obj);
}

$this->set_value(
$this->setValue(
$object,
$property,
[
$property => $objects
$property => $objects,
],
$mapping
);
Expand All @@ -289,7 +288,7 @@ protected function save_relations($object) {
foreach ($objects as $obj) {
$found = false;
foreach ($rows as $key => $row) {
if ($row[$mapping["lookup"]["mapper_column"]] == $obj->{$mapper::PRIMARY_KEY}) {
if ($row[$mapping['lookup']['mapper_column']] == $obj->{$mapper::PRIMARY_KEY}) {
// found a match. remove it from the db rows
$found = true;
unset($rows[$key]);
Expand All @@ -306,12 +305,12 @@ protected function save_relations($object) {
// delete lookup rows that are no longer needed
$pk_values = [];
foreach ($rows as $row) {
$pk_values[] = $row[$mapping["lookup"]["primary_key"]];
$pk_values[] = $row[$mapping['lookup']['primary_key']];
}
$this->crud->delete(
$mapping["lookup"]["table"],
$mapping['lookup']['table'],
[
$mapping["lookup"]["primary_key"] => $pk_values
$mapping['lookup']['primary_key'] => $pk_values,
]
);
}
Expand All @@ -320,10 +319,10 @@ protected function save_relations($object) {
// add new relational data
foreach ($add as $obj) {
$this->crud->create(
$mapping["lookup"]["table"],
$mapping['lookup']['table'],
[
$mapping["lookup"]["mapper_column"] => $obj->{$mapper::PRIMARY_KEY},
$mapping["lookup"]["foreign_column"] => $this->get_value($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY]),
$mapping['lookup']['mapper_column'] => $obj->{$mapper::PRIMARY_KEY},
$mapping['lookup']['foreign_column'] => $this->getValue($object, $this::PRIMARY_KEY, $this::MAPPING[$this::PRIMARY_KEY]),
]
);
}
Expand All @@ -343,13 +342,14 @@ protected function save_relations($object) {
*
* @return array
*/
protected function get_data($object) {
$data = array();
protected function getData($object) {
$data = [];
foreach ($this::MAPPING as $property => $mapping) {
if (empty($mapping["mapper"])) {
$data[$property] = $this->get_value($object, $property, $mapping);
if (empty($mapping['mapper'])) {
$data[$property] = $this->getValue($object, $property, $mapping);
}
}

return $data;
}
}
Loading

0 comments on commit 7facab2

Please sign in to comment.