66
77use DateTime ;
88use Doctrine \Bundle \FixturesBundle \Fixture ;
9- use Doctrine \DBAL \Connection ;
109use Doctrine \Persistence \ObjectManager ;
10+ use PhpList \Core \Domain \Model \Subscription \Subscriber ;
11+ use PhpList \Core \TestingSupport \Traits \ModelTestTrait ;
1112use RuntimeException ;
1213
1314class SubscriberFixture extends Fixture
1415{
16+ use ModelTestTrait;
17+
1518 public function load (ObjectManager $ manager ): void
1619 {
1720 $ csvFile = __DIR__ . '/Subscriber.csv ' ;
@@ -26,41 +29,25 @@ public function load(ObjectManager $manager): void
2629 }
2730
2831 $ headers = fgetcsv ($ handle );
29- if ($ headers === false ) {
30- throw new RuntimeException ('Could not read headers from CSV file. ' );
31- }
32-
33- /** @var Connection $connection */
34- $ connection = $ manager ->getConnection ();
35-
36- $ insertQuery = "
37- INSERT INTO phplist_user_user (
38- id, entered, modified, email, confirmed, blacklisted, bouncecount,
39- uniqid, htmlemail, disabled, extradata
40- ) VALUES (
41- :id, :creation_date, :modification_date, :email, :confirmed, :blacklisted, :bounce_count,
42- :unique_id, :html_email, :disabled, :extra_data
43- )
44- " ;
45-
46- $ stmt = $ connection ->prepare ($ insertQuery );
4732
4833 while (($ data = fgetcsv ($ handle )) !== false ) {
4934 $ row = array_combine ($ headers , $ data );
5035
51- $ stmt ->executeStatement ([
52- 'id ' => (int ) $ row ['id ' ],
53- 'creation_date ' => (new DateTime ($ row ['entered ' ]))->format ('Y-m-d H:i:s ' ),
54- 'modification_date ' => (new DateTime ($ row ['modified ' ]))->format ('Y-m-d H:i:s ' ),
55- 'email ' => $ row ['email ' ],
56- 'confirmed ' => (bool ) $ row ['confirmed ' ] ? 1 : 0 ,
57- 'blacklisted ' => (bool ) $ row ['blacklisted ' ] ? 1 : 0 ,
58- 'bounce_count ' => (int ) $ row ['bouncecount ' ],
59- 'unique_id ' => $ row ['uniqueid ' ],
60- 'html_email ' => (bool ) $ row ['htmlemail ' ] ? 1 : 0 ,
61- 'disabled ' => (bool ) $ row ['disabled ' ] ? 1 : 0 ,
62- 'extra_data ' => $ row ['extradata ' ],
63- ]);
36+ $ subscriber = new Subscriber ();
37+ $ this ->setSubjectId ($ subscriber ,(int )$ row ['id ' ]);
38+
39+ $ subscriber ->setEmail ($ row ['email ' ]);
40+ $ subscriber ->setConfirmed ((bool ) $ row ['confirmed ' ]);
41+ $ subscriber ->setBlacklisted ((bool ) $ row ['blacklisted ' ]);
42+ $ subscriber ->setBounceCount ((int ) $ row ['bouncecount ' ]);
43+ $ subscriber ->setHtmlEmail ((bool ) $ row ['htmlemail ' ]);
44+ $ subscriber ->setDisabled ((bool ) $ row ['disabled ' ]);
45+
46+ $ manager ->persist ($ subscriber );
47+ // avoid pre-persist
48+ $ subscriber ->setUniqueId ($ row ['uniqid ' ]);
49+ $ this ->setSubjectProperty ($ subscriber ,'creationDate ' , new DateTime ($ row ['entered ' ]));
50+ $ this ->setSubjectProperty ($ subscriber ,'modificationDate ' , new DateTime ($ row ['modified ' ]));
6451 }
6552
6653 fclose ($ handle );
0 commit comments