Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LQDN Adminsys
don
Commits
51d8b106
Commit
51d8b106
authored
Nov 15, 2018
by
Okhin
Browse files
Building the framework itemsfor Counterparts
parent
678e4014
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/LQDN/Command/CounterpartChangeStateCommand.php
0 → 100644
View file @
51d8b106
<?php
namespace
LQDN\Command
;
class
CounterpartChangeStateCommand
{
private
$counterpartId
;
private
$state
;
public
function
__construct
(
$counterpartId
,
$state
)
{
$this
->
counterpartId
=
$counterpartId
;
$this
->
state
=
$state
;
}
public
function
getCounterpartId
()
{
return
$this
->
counterpartId
;
}
public
function
getState
()
{
return
$this
->
state
;
}
}
src/LQDN/Command/CounterpartCreateCommand.php
View file @
51d8b106
...
...
@@ -2,7 +2,7 @@
namespace
LQDN\Command
;
class
Counter
P
artCreateCommand
class
Counter
p
artCreateCommand
{
private
$counterpartId
;
private
$adresseId
;
...
...
@@ -12,7 +12,7 @@ class CounterPartCreateCommand
private
$status
;
private
$date
;
public
function
__construct
(
$adress
e
Id
,
$userId
,
$quoi
,
$taille
,
$status
,
$date
)
public
function
__construct
(
$ad
d
ressId
,
$userId
,
$quoi
,
$taille
,
$status
,
$date
)
{
$this
->
userId
=
$userId
;
$this
->
addressId
=
$addressId
;
...
...
@@ -29,7 +29,7 @@ class CounterPartCreateCommand
public
function
getAddressId
()
{
return
$this
->
address
i
d
;
return
$this
->
address
I
d
;
}
public
function
getQuoi
()
...
...
src/LQDN/Command/CounterpartDeleteCommand.php
0 → 100644
View file @
51d8b106
<?php
namespace
LQDN\Command
;
class
CounterpartDeleteCommand
{
private
$counterpartId
;
public
function
__construct
(
$counterpartId
)
{
$this
->
counterpartId
=
$counterpartId
;
}
public
function
getCounterpartId
()
{
return
$this
->
counterpartId
;
}
}
src/LQDN/Finder/CounterpartFinder.php
View file @
51d8b106
...
...
@@ -56,4 +56,91 @@ EOQ;
return
$counterparts
;
}
/**
* Return all counterparts in a specific status
*
* @param mixed $status
*
* @return array
*/
public
function
findByStatus
(
$status
)
{
$status
=
(
int
)
$status
;
$counterparts
=
[];
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM contreparties WHERE status = :status'
,
[
'status'
=>
$status
]);
while
(
$counterpart
=
$stmt
->
fetch
())
{
$counterpart
[
'pdf_id'
]
=
''
;
$counterpart
[
'pdf_nom'
]
=
''
;
$counterpart
[
'pdf_url'
]
=
''
;
// Of course, piplomes are messy
// @TODO: This part is probably not working.
if
(
'piplome'
===
$counterpart
[
'quoi'
])
{
$query
=
<<<EOQ
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
EOQ;
$pdf
=
$this
->
connection
->
fetchAssoc
(
$query
,
[
'size'
=>
$counterpart
[
'taille'
]
]);
if
(
false
!==
$pdf
)
{
$counterpart
[
'pdf_id'
]
=
$pdf
[
'id'
];
$counterpart
[
'pdf_nom'
]
=
$pdf
[
'pseudo'
];
$counterpart
[
'pdf_url'
]
=
$pdf
[
'pdf'
];
}
}
$counterparts
[
$counterpart
[
'id'
]]
=
$counterpart
;
}
return
$counterparts
;
}
/** Return all counterparts by quoi
*
* @param mixed $quoi
*
* @return array
*/
public
function
findByQuoi
(
$quoi
)
{
$quoi
=
(
string
)
$quoi
;
$counterparts
=
[];
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM contreparties WHERE quoi = :quoi'
,
[
'quoi'
=>
$quoi
]);
while
(
$counterpart
=
$stmt
->
fetch
())
{
$counterpart
[
'pdf_id'
]
=
''
;
$counterpart
[
'pdf_nom'
]
=
''
;
$counterpart
[
'pdf_url'
]
=
''
;
// Of course, piplomes are messy
// @TODO: This part is probably not working.
if
(
'piplome'
===
$counterpart
[
'quoi'
])
{
$query
=
<<<EOQ
SELECT d.id as id, a.nom as pseudo, d.pdf as pdf
FROM dons d
JOIN adresses a ON a.id = d.adresse_id
JOIN contreparties c ON c.id = d.taille
WHERE d.taille = :size
EOQ;
$pdf
=
$this
->
connection
->
fetchAssoc
(
$query
,
[
'size'
=>
$counterpart
[
'taille'
]
]);
if
(
false
!==
$pdf
)
{
$counterpart
[
'pdf_id'
]
=
$pdf
[
'id'
];
$counterpart
[
'pdf_nom'
]
=
$pdf
[
'pseudo'
];
$counterpart
[
'pdf_url'
]
=
$pdf
[
'pdf'
];
}
}
$counterparts
[
$counterpart
[
'id'
]]
=
$counterpart
;
}
return
$counterparts
;
}
}
src/LQDN/Handler/AddressHandler.php
View file @
51d8b106
...
...
@@ -67,7 +67,6 @@ EOF;
* Check if an address already exists.
*
* @param int $userId
* @param string $alias
*
* @return bool
*/
...
...
src/LQDN/Handler/CounterpartHandler.php
View file @
51d8b106
...
...
@@ -5,7 +5,7 @@ namespace LQDN\Handler;
use
Doctrine\DBAL\Connection
;
use
LQDN\Command\CounterpartCreateCommand
;
use
LQDN\Command\CounterpartDeleteCommand
;
use
LQDN\Command\Counterpart
Upd
ateCommand
;
use
LQDN\Command\Counterpart
ChangeSt
ateCommand
;
use
LQDN\Exception\CounterpartAlreadyExistsException
;
class
CounterpartHandler
...
...
@@ -25,9 +25,10 @@ class CounterpartHandler
public
function
handleCounterpartDeleteCommand
(
CounterpartDeleteCommand
$command
)
{
$counterpartId
=
$command
->
getCounterpartId
();
// Let's check if the counterpart exist
$this
->
connection
->
executeUpdate
(
"DELETE FROM co
u
nt
e
rparts WHERE id = :id"
,
[
'id'
=>
$counterpartId
()
]
"DELETE FROM contr
e
part
ie
s WHERE id = :id"
,
[
'id'
=>
$counterpartId
]
);
}
...
...
@@ -39,18 +40,75 @@ class CounterpartHandler
public
function
handleCounterpartCreateCommand
(
CounterpartCreateCommand
$command
)
{
$userId
=
$command
->
getUserId
();
$adresseId
=
$command
->
getAdressId
();
$adresseId
=
$command
->
getAd
d
ressId
();
$query
=
<<<EOF
REPLACE
INTO co
u
nt
e
rparts(datec, user_id, ad
d
resse_id, quoi, taille, status)
VALUES (:datec, :user_id, :ad
d
resse_id, :quoi, :taille, :status)
INSERT
INTO contr
e
part
ie
s(datec, user_id, adresse_id, quoi, taille, status)
VALUES (:datec, :user_id, :adresse_id, :quoi, :taille, :status)
EOF;
$stmt
=
$this
->
connection
->
prepare
(
$query
);
$stmt
->
bindValue
(
'datec'
,
$command
->
getDateCreation
());
$stmt
->
bindValue
(
'user_id'
,
$command
->
getUserId
());
$stmt
->
bindValue
(
'adresse_id'
,
$command
->
getAddressId
());
$stmt
->
bindValue
(
'quoi'
,
$command
->
getQuoi
());
$stmt
->
bindValue
(
'taille'
,
$command
->
getTaille
());
$stmt
->
bindValue
(
'datec'
,
$command
->
getDate
());
$stmt
->
bindValue
(
'status'
,
$command
->
getStatus
());
$stmt
->
execute
();
}
/**
* Chnge the state of a counterpart
*
* @param CounterpartChangeStateCommand $command
*/
public
function
handleCounterpartChangeStateCommand
(
CounterpartChangeStateCommand
$command
)
{
$counterpartId
=
$command
->
getCounterpartId
();
$status
=
$command
->
getState
();
$this
->
connection
->
executeUpdate
(
"UPDATE contreparties SET status = :status WHERE id=:id"
,
[
'status'
=>
$status
,
'id'
=>
$counterpartId
,
]
);
}
/**
* Test if a counterpart exists
*
* @param int $counterpartId
*
* @return bool
*/
private
function
counterpartExists
(
$counterpartId
)
{
return
(
bool
)
$this
->
connection
->
fetchColumn
(
"SELECT 1 FROM contreparties WHERE id = :counterpart_id"
,
[
'counterpart_id'
=>
$counterpartId
,
],
0
);
}
/**
* Test if the counterpart can be deleted
*
* @param int $countepartId
*
* @return bool
*/
private
function
counterpartUsed
(
$counterpartId
)
{
$status
=
(
int
)
$this
->
connection
->
fetchColumn
(
"SELECT status FROM contreparties WHERE id = :id"
,
[
'id'
=>
$counterpartId
,
],
0
);
return
(
$status
==
2
);
// status 2 is delivered counterparts
}
}
tests/functional/Finder/CounterpartFinderTest.php
View file @
51d8b106
...
...
@@ -29,4 +29,50 @@ class CounterpartFinderTest extends FunctionalTest
];
$this
->
assertEquals
(
$expectedCounterpart
,
$firstCounterpart
);
}
public
function
testFindByQuoi
()
{
$counterparts
=
$this
->
container
[
'counterpart_finder'
]
->
findByQuoi
(
'piplome'
);
$this
->
assertCount
(
1
,
$counterparts
);
$firstCounterpart
=
reset
(
$counterparts
);
// Check the first counterpart
$expectedCounterpart
=
[
'id'
=>
'2'
,
'user_id'
=>
'2'
,
'datec'
=>
'2016-06-22 12:34:00'
,
'quoi'
=>
'piplome'
,
'taille'
=>
'2'
,
'status'
=>
'1'
,
'adresse_id'
=>
null
,
'pdf_id'
=>
'1'
,
'pdf_nom'
=>
'Main'
,
'pdf_url'
=>
'pdf'
,
'commentaire'
=>
''
,
];
$this
->
assertEquals
(
$expectedCounterpart
,
$firstCounterpart
);
}
public
function
testFindById
()
{
$counterparts
=
$this
->
container
[
'counterpart_finder'
]
->
findByStatus
(
1
);
$this
->
assertCount
(
2
,
$counterparts
);
$firstCounterpart
=
reset
(
$counterparts
);
$expectedCounterpart
=
[
'id'
=>
'1'
,
'datec'
=>
'2016-06-22 12:34:00'
,
'user_id'
=>
'1'
,
'quoi'
=>
'pishirt'
,
// [piplome|pibag|pishirt|hoodie]
'taille'
=>
'8'
,
'status'
=>
'1'
,
'adresse_id'
=>
'1'
,
'pdf_id'
=>
''
,
'pdf_nom'
=>
''
,
'pdf_url'
=>
''
,
'commentaire'
=>
''
,
];
$this
->
assertEquals
(
$expectedCounterpart
,
$firstCounterpart
);
}
}
tests/functional/Handler/CounterpartHandlerTest.php
0 → 100644
View file @
51d8b106
<?php
namespace
LQDN\Tests\Functional\Handler
;
use
LQDN\Command\CounterpartCreateCommand
;
use
LQDN\Command\CounterpartDeleteCommand
;
use
LQDN\Command\CounterpartChangeStateCommand
;
use
LQDN\Handler\CounterpartHandler
;
use
LQDN\Tests\Functional\FunctionalTest
;
class
CounterpartHandlerTest
extends
FunctionalTest
{
public
function
testCounterpartDelete
()
{
$this
->
assertTrue
(
$this
->
counterpartExists
(
1
));
$this
->
container
[
'command_handler'
]
->
handle
(
new
CounterpartDeleteCommand
(
1
));
}
public
function
testCounterpartCreate
()
{
$this
->
assertFalse
(
$this
->
counterpartExists
(
3
));
$this
->
container
[
'command_handler'
]
->
handle
(
new
CounterpartCreateCommand
(
1
,
1
,
'pishirt'
,
4
,
1
,
date
(
"Y-m-d H:i:s"
)));
}
public
function
testCounterpartChangeState
()
{
$this
->
assertTrue
(
$this
->
counterpartExists
(
1
));
$this
->
container
[
'command_handler'
]
->
handle
(
new
CounterpartChangeStateCommand
(
1
,
2
));
}
/**
* Check if a counterpart exists in BDD
*
* @param int $id
*
* @return bool
*/
private
function
counterpartExists
(
$id
)
{
return
(
bool
)
$this
->
container
[
'db'
]
->
fetchColumn
(
"SELECT 1 FROM contreparties WHERE id =
$id
"
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment